Thursday, November 12, 2009

Re: Switching a Models Field Class at Runtime

On Thursday 12 November 2009 06:05:00 Sean Brant wrote:
> Let me start out by saying this feels a little over engineered but I
> wanted to see what people thought anyways.
>
> I would like to create a list similar to a Facebook wall. You can post
> stuff and attach content to the post. In the past I have create a
> table with properties for all content formats and only set the one I
> was interested in. Thats bad cause you have all these empty fields for
> no reason. I have also just created foreign key to a separate content
> model (one for each format). That means you have to joins all the
> time. Since at the end of the day all of these formats are stored as
> strings, I can just use ONE content field. However I lose all the nice
> Image and File handling stuff Django gives you. So what if there was a
> ModelField that could change itself at runtime. Would I be better of
> doing this feature in a non-relational database and handling all the
> file upload stuff myself?
>
> All content will be represented in the database as a VARCHAR. I'll add
> some pseudo code that might explain it better than i can.
>
> choices = (
> (0, 'note'),
> (1, 'link'),
> (2, 'image'),
> )
>
> formats = (
> (0, models.CharField, {'max_length': 200}),
> (1, models.UrlField,),
> (2, models.ImageField, {'upload_to': 'uploads'),
> )
>
> class Item(models.Model):
> title = models.CharField(max_length=200)
> format = models.IntegerField(choices=choices, default=0)
> content = MultiFormatField(formats=formats, format_field='format')
> # changes based on instance.format
>
> Thanks for taking the time to read this over.
>
> - Sean
>
> --
How would generic.GenericForeignKey fit your needs?

http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#id1

Mikr


Bringing computers into the home won't change either one, but may
revitalize the corner saloon.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate