Answers for "django add foreign key to existing model"

3

django models add foreign key

class Person(models.Model):
    name = models.CharField(max_length=50)
    birthday = models.DateField()
    anniversary = models.ForeignKey(Anniversary, on_delete=models.CASCADE)
    address = models.ForeignKey(Address, on_delete=models.DO_NOTHING)

class Address(models.Model):
    line1 = models.CharField(max_length=150)
    line2 = models.CharField(max_length=150)
    postalcode = models.CharField(max_length=10)
    city = models.CharField(max_length=150)
    country = models.CharField(max_length=150)

class Anniversary(models.Model):
    date = models.DateField()
Posted by: Guest on August-30-2021
0

how to get foregine key field from models

>>> SubCategory._meta.get_field('category').remote_field.model.__name__ 
>>> 'Category'
Posted by: Guest on January-12-2021

Code answers related to "django add foreign key to existing model"

Python Answers by Framework

Browse Popular Code Answers by Language