I have a model which has all the fields as a foreign key to other models. How can we create test case in Django rest framework in that case??
I have a model as follows:
class Example(models.Model):
package = models.ForeignKey(
Destination, related_name="packages", on_delete=models.CASCADE
)
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
null=True,
related_name="user_packages",
)
tour = models.ForeignKey(
Tours,
on_delete=models.CASCADE,
null=True,
related_name="tour_packages",
)
In a model, when there is just one field in a Django model, it can be done in the following way:
class NewsLetter(models.Model):
NewsLetterID = models.AutoField(primary_key=True)
Email = models.CharField(max_length=255)
Connected = models.BooleanField(default=False)
UserID = models.ForeignKey(User, on_delete=models.CASCADE)
class Meta:
db_table = 'NewsLetter'
@classmethod
def setUpTestData(cls):
#Set up non-modified objects used by all test methods
user = User.objects.create(<fill params here>)
NewsLetter.objects.create(NewsLetterID=1, Email='test@test.com', Connected=False,UserID=user)
So even if I have created all the objects for all the foreign-key fields just like this example, the thing is, for the related field models, they themselves have foreign key fields. How can we approach this?? For example in my Example model, the Destination model is itself related to other foreign key fields. So how to create unit test for create api for this Example model??