lkuessner / patreon-clone-api

2 stars 0 forks source link

feat(users address): add address tables in relation with our custom `User` model #6

Open Blankscreen-exe opened 1 month ago

Blankscreen-exe commented 1 month ago

Scope

Adding related tables to custom Users table This issue is to be performed after #5 is finished.

Description

Create a new model inside users app which is named as UserAddress. It is self explanatory what this model will contain. For now , just create the following fields:

All three of these fields are Foreign Keys from other tables. That means you will also need to create three more tables:

📌 Note: You can use migrations as seeders to populate the Country, City and State. Learn more about this from this stackoverflow post. As for JSON data of the countries, cities annd states, can be generated using online tools.

References

NOTE: This is a reference code, DO NOT use it as is.


class Country(models.Model):
    title = models.CharField(_("Title"), max_length=255)
    code = models.CharField(_("Code"), max_length=255)
    class Meta:
        verbose_name_plural = "Countries"

    def __str__(self) -> str:
        return self.title

class State(models.Model):
    title = models.CharField(_("Title"), max_length=255)
    country = models.ForeignKey(Country, related_name='state_country', on_delete=models.SET_NULL, null=True)
    def __str__(self) -> str:
        return self.title

class City(models.Model):
    title = models.CharField(_("Title"), max_length=255)
    state = models.ForeignKey(State, related_name='city_state', on_delete=models.SET_NULL, null=True)
    class Meta:
        verbose_name_plural = "Cities"

    def __str__(self) -> str:
        return self.title