Django Models Questions

I think you can access data from the original model in django when you use a foreign key in another model to reference the original model, I hope that is correct? Kinda like SQL JOIN
Also, can I run Django shell commands directly in the python views.py file by importing the necessary model, or can the stuff be done only in django shell?

Yes, in particular by using related_name https://docs.djangoproject.com/en/3.2/ref/models/fields/#django.db.models.ForeignKey.related_name

Yes, of course you can, and it’s quite common

Thanks! I was actually trying to look up some stuff regarding related_name yesterday, the concept made sense to me, but the syntax didn’t fully, I think you can access the properties by doing something like model1.related_name_provided_to_the_forgeign_key.model2_attribute. I was confused about the django shell thing since it seemed like the most obvious thing to use views.py for the models, but nearly everybody I had seen was using django shell, and I think using views.py was probably too obvious to everybody except my REALLY BIG BRAIN, henceforth, google wasn’t of much help.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.