Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Soory for my delay (i had other tasks).</p> <p>I solved that problem not with proposed libraries (with simple javascript), but exist another problem: when i changed country - select region changes automatically, but i can't save new region value (when another region from the same country - not problem), because error <strong>Select a valid choice. That choice is not one of the available choices.</strong></p> <p>Models are the same in first question, but default value for model country equal 1.</p> <p>My first way was change country select through formfield_key function, </p> <pre><code>def formfield_for_foreignkey(self, db_field, request, **kwargs): if db_field.name == 'region': kwargs["queryset"] = Region.objects.filter(country=self.country) return db_field.formfield(**kwargs) </code></pre> <p>but i don't know is the saving objects, or editing objects.</p> <p><a href="https://stackoverflow.com/a/949290/1221313">Here</a> wrote - the best way is changing through form and now i have code:</p> <pre><code>class Item(models.Model): country = models.ForeignKey(Country, default=1) region = models.ForeignKey(Region, related_name='') class ItemAdmin(admin.ModelAdmin): form = CustomItemForm prepopulated_fields = {"alias": ("name",)} list_filter = ('country', 'category',) class CustomItemForm(forms.ModelForm): def __init__(self, *args, **kwargs): try: country = kwargs['instance'].country except KeyError: country = 1 super(CustomItemForm, self).__init__(*args, **kwargs) self.fields['region'].queryset = Region.objects.filter(country=country) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload