Answers for "how to save a record with foreign key in django"

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

django insert data into database foreign key view.py

for i in range(1,10):
        obj = Count.objects.filter(userId=request.user.id, channelId=cid)
        if not obj:
            news_obj = News_Channel.objects.get(id=i)
            o = Count.objects.create(id=Count.objects.all().count() + 1,userId=request.user.id, channelId=news_obj,rate=0)
            o.save()
        i += 1
Posted by: Guest on August-12-2020

Code answers related to "how to save a record with foreign key in django"

Python Answers by Framework

Browse Popular Code Answers by Language