copser
copser

Reputation: 2641

cities, country and zip_code models fields and modelsforms with django-cities

I'm trying to make three models fields cities, country and zip_code in my model so I can build a ModelForm and setup some custom signup, and for that I'm using dajngo-cities library.

I've configured everything I need for the app in my project and I've done the migrations, so I have loaded US cities, countries and zip_codes in the system, but now I'm failing to understand how can I build this model fields so I can make dropdown select buttons in my forms, can someone please explain me how to build this properly, thanks.

Upvotes: 0

Views: 464

Answers (1)

Dev Aggarwal
Dev Aggarwal

Reputation: 8516

I strongly suggest you read django's documentation. The example that they used in the docs is extremely similar to your use-case.

Here is a little hint - You actually need to make 2 Models. This way you can have a Many to One database relation between City and Country models, which is exactly what you need.

from django.db import models

class Country(models.Model):
    country = models.CharField(max_length=200)    


class City(models.Model):
    zip_code = models.IntegerField(default=0)
    country = models.ForeignKey(Country, on_delete=models.CASCADE)

Upvotes: 1

Related Questions