Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading