How to link address model to views

◇◆丶佛笑我妖孽 提交于 2019-12-06 15:14:40

use a foreign key to point to your address model:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    nick_name = models.CharField('Nick name', max_length=30, blank=True, default='')
    bio = models.TextField(max_length=500, blank=True)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')
    addresses = models.ForeignKey(Address) # <-- fix here

Hope this helps!

You should declare ForeignKey with '<app>.<model>' format:

class AddressType(models.Model):   
    address = models.ForeignKey('yourapp.Address', on_delete=models.CASCADE)
    profile = models.ForeignKey('yourapp.Profile', on_delete=models.CASCADE)

or directly give the class:

    address = models.ForeignKey(Address, on_delete=models.CASCADE)
    profile = models.ForeignKey(Profile, on_delete=models.CASCADE)

Both of the other answers were incorrect, I ended up modifying everything and also creating a new model, here it is:

class Address(models.Model):
    name = models.CharField(max_length=100, blank=False)
    address1 = models.CharField("Address lines 1", max_length=128)
    address2 = models.CharField("Address lines 2", max_length=128, blank=True)
    city = models.CharField("City", max_length=64)
    # state = USStateField("State", default='FL')
    state = models.CharField("State", max_length=128, default='FL')
    zipcode = models.CharField("Zipcode", max_length=5)
    user = models.ForeignKey(Profile, on_delete=models.CASCADE, blank=False)

    class Meta:
        verbose_name_plural = 'Address'

    def __str__(self):
        return self.name
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!