I'm working in *Django 1.11*

I have three apps, *country*, *state* and *address*

*state* model is associated with *country* by foreign key and *address* is 
associated with *state* with a foreign key.

*country/models.py*

    class Country(models.Model):
        name = models.CharField(max_length=100)
        code = models.CharField(max_length=50)
    
        class Meta:
            db_table = 'countries'
    
        def __str__(self):
            return self.name




*state/models.py*

    class State(models.Model):
        country = models.ForeignKey(Country, on_delete=models.CASCADE)
        name = models.CharField(max_length=100)
        code = models.CharField(max_length=50)
    
        class Meta:
            db_table = 'states'
    
        def __str__(self):
            return self.name



*address/models.py*

    class Address(models.Model):
        line_1 = models.CharField(max_length=200)
        line_2 = models.CharField(max_length=200)
        city = models.CharField(max_length=200)
        state = models.ForeignKey(State, on_delete=models.PROTECT)
        postal_code = models.CharField(max_length=15)
    
        class Meta:
            db_table = 'addresses'
    
        def __str__(self):
            return '%s, %s, %s, %s' % (self.line_1, self.line_2, self.city, 
self.state)



Since one country can have multiple states, I want user to be able to 
select state after selecting country, which will list related/associated 
states while adding/editing address.

I also want this feature in */admin* as app is registered to admin too.

*Is there any simple and better way to do this?*

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b2d37648-b6cb-4cac-97cc-4fad30bf2b0a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to