Table1 where i get the data from a management script that calls a certain API. Table2 also gets data from an Api. The scenario is xid & kid are same, so i have implemented a OneToOne relationship so that i can easily get the related item from table1 in context of table2. Table1 has to be populated in order to populate table1, as only then assignment of table2 instance can be done. 5687 Already exists in table1 but still i get the error:
ValueError: Cannot assign "5687": "Table2.kid" must be a "Table1" instance.
Models.py
class Table1(models.Model):
xid = models.IntegerField(primary_key=True)
amount = models.IntegerField()
class Table2(models.Model):
kid = models.OneToOneField(Table1,primary_key=True, on_delete=models.CASCADE)
vid = models.IntegerField(null=True, blank=True)
g_amount = models.IntegerField()
This is my script code that is same for both management commands that get me data.
Management/commands/script.py
""" API CODE """
data = {
'kid': kid,
'vid': vid,
'g_amount': g_amount
}
if Table2.objects.filter(vid=xid).exists() or Table1.objects.filter(xid=kid).exists() is False:
continue
else:
# print(data)
instance = Table2(**dataff)
instance.save()
Or should is there a way i don't need to have OneToOne relationship. Using Django's ORM a query can be constructed that will get combine the instance from these two table in one and place it in a Virtual Table so that i don't loose ORM capabilities.
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)