Hey Léon,

ChoiceField is a form field, not a table column—maybe you're thinking
about CommaSeparatedIntegerField? You could use that and hardcode
which categories represent which ids in your python code.
http://docs.djangoproject.com/en/dev/ref/models/fields/#commaseparatedintegerfield

On the other hand, the standard way of representing an object and its
categories in SQL is to use a ManyToMany relation. This approach
easily handles everything you want to do in a much easier to manage
format, and it's unlikely that you'll need to worry about "bloat"—
optimizing how you serve web pages, static files, caching, etc. will
give you *many* more gains than holding off from adding another table
to your db.

To setup your category, you can add something like this to your models
file (above your photos model):

class Category(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(unique=True)
    class Admin:
        pass

and then inside your photos model add a new column:

    categories = models.ManyToManyField(Category, blank=True)


-Peter

On Sep 9, 11:54 am, Léon Dignòn <[email protected]> wrote:
> Hello,
>
> let's assume we have a model for photos with the fields id, user,
> photo. Any user should be able to categorize his foto with a given
> number of categories, e.g. person, car, building. In future it's
> possible that I add more categories.
>
> My question is whether the new category field should be a ChoiceField,
> or a ForeignKey?
>
> I would prefer a ChoiceField because
> - I am 99% sure that there won't be new categories.
> - the only person who maybe adds a new category will be me.
> - another model would bloat up my database and my models.py
>
> But maybe I should use a ForeignKey because,
> - I don't have to edit the model/table.
> - it's easier to add a choice to a table than in the source code.
> - because values in databases should be atomic.
>
> Otherwise, assuming that syncdb can't handle adding new choices to an
> existing table, adding a choice directly in the database application
> (MySQL, PostgreSQL) is no problem. Removing a choice won't happen.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to