Home > Blockchain >  Difference Django DB models using the ForeignKey to object with or without "to" statement?
Difference Django DB models using the ForeignKey to object with or without "to" statement?

Time:04-07

I am new to Django and trying to understand someone else code. Where I am struggling with is the models.py and when to use a direct assignment of another object or when to use the "to" statement.

What is the difference between those statement?

model = models.ForeignKey('Car', on_delete=models.CASCADE, blank=True, null=True)

model = models.ForeignKey(to='Car', on_delete=models.CASCADE, blank=True, null=True)

CodePudding user response:

The use of to is implicit for the first parameter ('Car'), I would omit it but it is also acceptable to explicitly include it. So those statements are equivalent.

  • Related