How to create dynamic models in django??

1,960 views
Skip to first unread message

Prashanth Patelc

unread,
Feb 26, 2022, 10:42:55 PM2/26/22
to django...@googlegroups.com
How to create dynamic models in django rest framework?
Is there any chance to create dynamic models with APIs

Any examples please send me thanks in advance..

1) Requirement is need create table name and fields in frontend
2) we are  getting the data and store in to the db create db structure
3) get the table name and fields create table in backend &postgresql store to
4)this code don't update or add into the models 
5)store the data into the tables 
4)get the data into the tables using orm or any raw queries

Antonis Christofides

unread,
Feb 27, 2022, 2:00:56 AM2/27/22
to django...@googlegroups.com

Short answer: I don't know. It probably isn't straightforward, but it's doable in principle.

Longer answer: Django hasn't been designed with this case in mind. It's possible it's not the appropriate tool for the job (but again it might be, I don't know). In my experience you should avoid doing such things—it's likely to lead to an unmanageable code mess. Keep it simple. Consider working without a relational database.

Regards,

Antonis

Antonis Christofides
+30-6979924665 (mobile)
--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMCU6Coop%3DVU67%2B1Ui%2BswvA2xijUYcre%2B%3D-Z_CzLnf-1pRRXUw%40mail.gmail.com.

Heman Okumbo

unread,
Feb 27, 2022, 11:42:26 AM2/27/22
to django...@googlegroups.com

Steve Smith

unread,
Feb 27, 2022, 12:56:58 PM2/27/22
to django...@googlegroups.com
My two cents.  I've been trying to do a very similar thing...I've accomplished it in Django....using Formsets....It was painful....the documentation on Formsets is scarce in my opinion and to get them to work is a lot of trial and error, not to mention complexity with corresponding Javascript.  At least for me it was.  I'm not using the rest framework as it relates to a table and rows but I am using them with a server based Django installation....so I'm not sure how this applies to your use case.  I have recently started experimenting with HTMX....to ultimately replace formsets and I'm pretty close to having it figured out.  Again with HTMX, there's not a lot of documentation so it's been a ton of trial and error....If any of this resonates with you...let me know and I can provide some more specifics.

Steve


From: django...@googlegroups.com <django...@googlegroups.com> on behalf of Antonis Christofides <ant...@antonischristofides.com>
Sent: Sunday, February 27, 2022 12:58 AM
To: django...@googlegroups.com <django...@googlegroups.com>
Subject: Re: How to create dynamic models in django??
 

Shaheed Haque

unread,
Feb 27, 2022, 4:32:47 PM2/27/22
to django...@googlegroups.com

Clive Bruton

unread,
Feb 28, 2022, 6:57:28 AM2/28/22
to django...@googlegroups.com

On 27 Feb 2022, at 17:54, Steve Smith wrote:

> ...the documentation on Formsets is scarce in my opinion and to get
> them to work is a lot of trial and error...

To massively simplify formsets try django-extra-views


-- Clive

Prashanth Patelc

unread,
Feb 28, 2022, 11:27:59 AM2/28/22
to django...@googlegroups.com
Any example?

--
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.

Clive Bruton

unread,
Feb 28, 2022, 12:08:32 PM2/28/22
to django...@googlegroups.com

On 28 Feb 2022, at 16:24, Prashanth Patelc wrote:

> Any example?

Just read the django-extra-views documentation. Essentially a formset
can be as simple to use as any of the built-in class-based views, eg:

from extra_views import InlineFormSetView

class EditContactAddresses(InlineFormSetView):
model = Contact
inline_model = Address
template_name = …


https://django-extra-views.readthedocs.io/en/latest/pages/formset-
views.html#inlineformsetview


-- Clive

Ryan Nowakowski

unread,
Mar 9, 2022, 6:55:18 PM3/9/22
to django...@googlegroups.com
The WQ project uses the Entity-Attribute-Value data model:

https://v1.wq.io/1.2/docs/eav-vs-relational

Derek

unread,
Mar 11, 2022, 12:48:57 AM3/11/22
to Django users
I'm not quite sure how these requirements have arisen as you have not provided any context as to the actual user or business needs.  In my experience,  most businesses and science organisations  understand very clearly what kinds of data they need to store and what they need to do with that data.   This makes it possible for the developers to design and  implement well-structured SQL  databases.

If,  for some reason, this is really not your use case then, as the other commenters have suggested,  it's likely that Django is not a good fit for your needs.  Perhaps you should consider a NoSQL  solution (https://www.mongodb.com/nosql-explained/when-to-use-nosql) instead;  and explore the use of  its associated technologies which can meet your stated requirements.

Joel Tanko

unread,
Mar 14, 2022, 9:41:33 AM3/14/22
to django...@googlegroups.com
If I understand correctly you're trying to give the user the power to create tables from the front end by saving his params to the database and using that data to create a table in another or the same  database, I tried doing the same with forms here's a tweak, hope it's what you're looking for or at least a step in the right direction. 

#models.py


Model = models.Model

class DynamicModel(Model):
    name = models.CharField(max_length=100)
    fields = models.ManyToManyField("Field", blank=True)

    def get_data(self, **kwargs) :
        # you'd probably want to do some 
           kind of db query here to postgres 
        using a Python wrapper. 
       #... cursor.execute("select * from %s" % self.name)
      # assuming that you've instantiated a postgres db cursor 

class Field(Model):
     name = models.CharField(max_length=100)
     field_type = models.CharField(max_length=100, 
    choices=(("integer", "Integer"), ("char", "char" ) )






--
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.
Reply all
Reply to author
Forward
0 new messages