Creating Dynamic Models

2012-06-11 Thread Karandeep Nagra
Hi,

New user to Django.  I have a class 'Server' and I want to set up my Django 
site so that when a server is added through the admin interface, an 
additional model along with its admin.ModelAdmin class are added.  So say a 
server 'Crushinator' is added to the database of servers.  At its creation, 
I want to create the model 'Crushinator_Users' and add the admin interface 
for this model.  Any ideas on how to accomplish this?  I tried using 
__init__ under the Server model, but it causes an attribute error when 
trying to create the server ("'Server' object has no attribute 'id'").


Any help is appreciated,
Karandeep Nagra

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/z-GDAnwq57UJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Creating dynamic models?

2009-11-26 Thread Kevin Renskers
Hi Matthias ,

Thank you, I went for option 1 and it works perfectly!

Funny, the add_to_class function basically does "setattr(cls, name,
value)". So I had the right solution, only you have to do this after
class creation, not inside its __init__ function. Good to know, will
blog about this :)

Again, thank you very much.

Cheers,
Kevin


On Nov 26, 10:14 am, Matthias Kestenholz
 wrote:
> On Thu, Nov 26, 2009 at 9:56 AM, Kevin Renskers  wrote:
> > Just a small update: the DynamicModels way as described on the wiki
> > doesn't work (it also says that it only works in Django 0.96, so
> > yeah..).
>
> > If anyone has any idea how to do this, I would be very thankful!
>
> > On Nov 24, 2:35 pm, Kevin Renskers  wrote:
> >> Hi all,
>
> >> In my Django project I want to have a model that is dynamically
> >> created. I tried using the __init__ function for this,  something like
> >> so:
>
> >> fields = ['field_a', 'field_b', 'field_c']
>
> >> class MyModel(models.Model):
> >>     def __init__(self, *args, **kwargs):
> >>         for field in fields:
> >>             setattr(self, field, models.DecimalField(decimal_places=4,
> >> max_digits=10))
>
> >> Sadly, this doesn't work. The columns don't get created when you run
> >> the syncdb command, and even something like
> >> MyModel._meta.get_all_field_names() doesn't return the dynamic fields.
>
> >> So, is there a way I can create a "dynamic" model? I did come 
> >> acrosshttp://code.djangoproject.com/wiki/DynamicModelsbutI don't really
> >> get that. It looks so much different then normal models, it seems like
> >> I would loose a lot of functionality or would have to change a lot of
> >> code somewhere else in my application?
>
> >> Hopefully there is an easy way to do this :)
>
> I can see two ways to achieve what you seem to want:
>
> 1. Add fields after the model has been created
>
> This method uses an only unofficially documented feature[1] of
> Django's model field classes.
>
> class MyModel(models.Model):
>     # a few fields
>
> for field in fields:
>     MyModel.add_to_class(field, models.DecimalField(decimal_places=4,
> max_digits=10))
>
> 2. Construct a new type dynamically
>
> class Meta:
>     verbose_name = _('my model')
>
> attrs = {
>     '__module__': 'mymodule',
>     'Meta': Meta,
>     'method1': method1,
>     # ... more fields and methods
>
> }
>
> for field in fields:
>     attrs[field] = models.DecimalField(...)
>
> MyModel = type('MyModel', (models.Model,), attrs)
>
> Of course, the usual caveats apply. It might make your code harder to
> read and understand, and harder to debug too, because it is not clear
> what model fields exist by simply looking at the model code (that
> applies especially to method 1)
>
> Matthias
>
> [1]: It's documented in Marty Alchin's excellent Pro Django book. I
> think we can assume that this method won't go away without very good
> reasons(tm).

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.




Re: Creating dynamic models?

2009-11-26 Thread Matthias Kestenholz
On Thu, Nov 26, 2009 at 9:56 AM, Kevin Renskers  wrote:
> Just a small update: the DynamicModels way as described on the wiki
> doesn't work (it also says that it only works in Django 0.96, so
> yeah..).
>
> If anyone has any idea how to do this, I would be very thankful!
>
>
> On Nov 24, 2:35 pm, Kevin Renskers  wrote:
>> Hi all,
>>
>> In my Django project I want to have a model that is dynamically
>> created. I tried using the __init__ function for this,  something like
>> so:
>>
>> fields = ['field_a', 'field_b', 'field_c']
>>
>> class MyModel(models.Model):
>>     def __init__(self, *args, **kwargs):
>>         for field in fields:
>>             setattr(self, field, models.DecimalField(decimal_places=4,
>> max_digits=10))
>>
>> Sadly, this doesn't work. The columns don't get created when you run
>> the syncdb command, and even something like
>> MyModel._meta.get_all_field_names() doesn't return the dynamic fields.
>>
>> So, is there a way I can create a "dynamic" model? I did come 
>> acrosshttp://code.djangoproject.com/wiki/DynamicModelsbut I don't really
>> get that. It looks so much different then normal models, it seems like
>> I would loose a lot of functionality or would have to change a lot of
>> code somewhere else in my application?
>>
>> Hopefully there is an easy way to do this :)
>>

I can see two ways to achieve what you seem to want:

1. Add fields after the model has been created

This method uses an only unofficially documented feature[1] of
Django's model field classes.

class MyModel(models.Model):
# a few fields

for field in fields:
MyModel.add_to_class(field, models.DecimalField(decimal_places=4,
max_digits=10))



2. Construct a new type dynamically

class Meta:
verbose_name = _('my model')

attrs = {
'__module__': 'mymodule',
'Meta': Meta,
'method1': method1,
# ... more fields and methods
}

for field in fields:
attrs[field] = models.DecimalField(...)

MyModel = type('MyModel', (models.Model,), attrs)




Of course, the usual caveats apply. It might make your code harder to
read and understand, and harder to debug too, because it is not clear
what model fields exist by simply looking at the model code (that
applies especially to method 1)


Matthias

[1]: It's documented in Marty Alchin's excellent Pro Django book. I
think we can assume that this method won't go away without very good
reasons(tm).

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.




Re: Creating dynamic models?

2009-11-26 Thread Kevin Renskers
Just a small update: the DynamicModels way as described on the wiki
doesn't work (it also says that it only works in Django 0.96, so
yeah..).

If anyone has any idea how to do this, I would be very thankful!


On Nov 24, 2:35 pm, Kevin Renskers  wrote:
> Hi all,
>
> In my Django project I want to have a model that is dynamically
> created. I tried using the __init__ function for this,  something like
> so:
>
> fields = ['field_a', 'field_b', 'field_c']
>
> class MyModel(models.Model):
>     def __init__(self, *args, **kwargs):
>         for field in fields:
>             setattr(self, field, models.DecimalField(decimal_places=4,
> max_digits=10))
>
> Sadly, this doesn't work. The columns don't get created when you run
> the syncdb command, and even something like
> MyModel._meta.get_all_field_names() doesn't return the dynamic fields.
>
> So, is there a way I can create a "dynamic" model? I did come 
> acrosshttp://code.djangoproject.com/wiki/DynamicModelsbut I don't really
> get that. It looks so much different then normal models, it seems like
> I would loose a lot of functionality or would have to change a lot of
> code somewhere else in my application?
>
> Hopefully there is an easy way to do this :)
>
> Cheers,
> Kevin

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.




Creating dynamic models?

2009-11-24 Thread Kevin Renskers
Hi all,

In my Django project I want to have a model that is dynamically
created. I tried using the __init__ function for this,  something like
so:

fields = ['field_a', 'field_b', 'field_c']

class MyModel(models.Model):
def __init__(self, *args, **kwargs):
for field in fields:
setattr(self, field, models.DecimalField(decimal_places=4,
max_digits=10))

Sadly, this doesn't work. The columns don't get created when you run
the syncdb command, and even something like
MyModel._meta.get_all_field_names() doesn't return the dynamic fields.

So, is there a way I can create a "dynamic" model? I did come across
http://code.djangoproject.com/wiki/DynamicModels but I don't really
get that. It looks so much different then normal models, it seems like
I would loose a lot of functionality or would have to change a lot of
code somewhere else in my application?

Hopefully there is an easy way to do this :)

Cheers,
Kevin

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.