Is it possible to create models inside a database model

35 views
Skip to first unread message

aditya

unread,
Jul 10, 2018, 11:26:55 AM7/10/18
to Django users
I have created a model Named Gallery and set upload path to media folder and in that folder I have 15 more folders with different images.
Now I want to access each folder and images present inside it using database.
My question is :
Is it possible to create 15 model inside an model and if possible, then How to access them in my Html file? If not then what should I do to show them in my Html file ?

Alexander Joseph

unread,
Jul 10, 2018, 12:23:51 PM7/10/18
to Django users
Good question - I'm looking for a similar solution myself. My application is a purchase order app so that a purchase order has details about the purchase order but also has an unspecified number of objects that belong to it (purchase items).
The only way I can think this MIGHT work is making a model for each, including a foreignKey to relate the puchase items back to the purchase order, and possibly putting both in the same CreateView/DetailView/UpdateView/etc. (if this is possible). I'll keep you updated if I find any better solutions though

Alexander Joseph

unread,
Jul 10, 2018, 12:59:55 PM7/10/18
to Django users
Also- this is assuming you're using CBVs, if youre just using FBVs its probably even easier on the view side, the model side would be the same

aditya gupta

unread,
Jul 10, 2018, 1:30:00 PM7/10/18
to django...@googlegroups.com
I am using CBVs in model.py file and FBVs in views.py file. 
As in image :-

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/10f8139f-ecd3-44d2-a7a6-9b6e7e2329ae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
model.jpg

Alexander Joseph

unread,
Jul 10, 2018, 3:12:22 PM7/10/18
to Django users
Oh ok I see. The related_name is what you use to access the specific field in the template so it needs to be unique to the model field. When you use the ForeignKey field type it uses the primaryKey of the related database models automatically so you dont need to worry about actually telling it which field to use to relate to in a RDBMS sense.

aditya gupta

unread,
Jul 10, 2018, 3:20:14 PM7/10/18
to django...@googlegroups.com
Well I was thinking is that instead of using related_name is there any other way I can write and call my images inside css lightbox for the slide show.

mottaz hejaze

unread,
Jul 11, 2018, 7:11:02 AM7/11/18
to django...@googlegroups.com
for alexander .. you can make three models .. one for orders .. one for items .. and one for orderedItems .. the orderedItems model will have relations to  both models items and  orders

mottaz hejaze

unread,
Jul 11, 2018, 7:33:54 AM7/11/18
to django...@googlegroups.com
you cant make models inside models becouse you cant make tables inside tables ... a model is a database table
 
aditya the easiest way is to redesign your model and make it of 15 fields ..each field will upload to a specific folder inside the media folder 


another way . is to make a one image field and another char field . and pass a choices to it . the choices will be a tuple with subfolder name inside media folder . the make the path inside the image field dynamic comming from that tuple of choices .. 

i didn't make something similar but you can give it a try
Reply all
Reply to author
Forward
0 new messages