Populate choices from model

1,581 views
Skip to first unread message

Gerd Koetje

unread,
Aug 21, 2013, 11:24:11 AM8/21/13
to django...@googlegroups.com
Hi all,

How do i populate choices from a model class inside models.py

doenst work:

regio = models.CharField(max_length=100, choices=list(Regio), default='Amsterdam')
regio = models.CharField(max_length=100, choices=Regio, default='Amsterdam')



C. Kirby

unread,
Aug 21, 2013, 11:33:57 AM8/21/13
to django...@googlegroups.com
If you want your field to represent another model you should be using a foreign key, not a CharField

regio = models.ForeignKey(Regio, default = Regio.objects.get(name = "Amsterdam"))

Rafael E. Ferrero

unread,
Aug 21, 2013, 11:34:16 AM8/21/13
to django...@googlegroups.com
look here
https://docs.djangoproject.com/en/1.5/ref/models/fields/#django.db.models.Field.choices

"... note that choices can be any iterable object – not necessarily a list or tuple. This lets you construct choices dynamically. But if you find yourself hacking choices to be dynamic, you’re probably better off using a proper database table with a ForeignKey. choices is meant for static data that doesn’t change much, if ever. "

See ya!


2013/8/21 Gerd Koetje <deonline...@gmail.com>

--
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 http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.



--
Rafael E. Ferrero
Claro: (03562) 15514856

Gerd Koetje

unread,
Aug 21, 2013, 11:44:55 AM8/21/13
to django...@googlegroups.com

    regio = models.CharField(max_length=100, choices=Regio, default='Amsterdam')

    land = models.CharField(max_length=100, choices=Landen, default='Nederland')


(django-env)[root@python pythondating]# python manage.py syncdb
CommandError: One or more models did not validate:
profielen.profielen: "regio": "choices" should be iterable (e.g., a tuple or list).
profielen.profielen: "land": "choices" should be iterable (e.g., a tuple or list).


it doesnt seem to accept a object




C. Kirby

unread,
Aug 21, 2013, 11:58:37 AM8/21/13
to django...@googlegroups.com
Why are you trying to link to another model with a CharField and not a ForeignKey? You seem to be trying to do something that is almost certainly not the way is should be done. Perhaps you should explain what you are trying to accomplish.

Gerd Koetje

unread,
Aug 21, 2013, 12:14:16 PM8/21/13
to django...@googlegroups.com
im building a prety complicated user detail form with all kinds of fields, like:  gender,haircolor,skincolor

some fields have to become dropdowns with only 1 choise out of many
and some fields have to become multiple select fields with alot of choise (multiple choises allowed)
I was trying to use 1 model that holds all the values for each field, but it seem to only work if i make a model for each field values

the dropdowns seem to work ok with foreignkey

Rafael E. Ferrero

unread,
Aug 21, 2013, 2:52:22 PM8/21/13
to django...@googlegroups.com
In djangoproject link its this example...

MEDIA_CHOICES = (
    ('Audio', (
            ('vinyl', 'Vinyl'),
            ('cd', 'CD'),
        )
    ),
    ('Video', (
            ('vhs', 'VHS Tape'),
            ('dvd', 'DVD'),
        )
    ),
    ('unknown', 'Unknown'),
)

Maybe you can use something like this:

YOUR_CHOICES = (
       ('haircolor', (
               ('blond', 'Blond'),
               ('black', 'Black),
            )
       ),

       ('skincolor', (
                ('white', 'White'),
                ('black', 'Black'),
            )
       ),

       ('Gender', (
                ('male', 'Male'),
                ('female', 'Female'),
            )
       ),
)

However, if you need dinamic choices use foreignkeys, if you need to many foreignkeys for your choices why not think in categories or tags... so design a m2m model

On my work have to catalog business contacts... i design a Contacts Model, a Categories Model and a model in the middle so a Contact can have one or more Categories.
Lets say that John Doe can have the next list of categories 'Brown Hair Color', 'White Skin Color', 'Blue Eyes Color', 'Male Sex'...




2013/8/21 Gerd Koetje <deonline...@gmail.com>

--
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 http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

Gerd Koetje

unread,
Aug 21, 2013, 3:14:35 PM8/21/13
to django...@googlegroups.com
i would vote for the m2m model, but i think i loose it there a bit

Rafael E. Ferrero

unread,
Aug 21, 2013, 5:05:54 PM8/21/13
to django...@googlegroups.com
I design a model structure to persist restrictions to a selectable category (i dont say that its ok... its for shore that can do a lot of improvements), and make a module to admin categories with that if i choose some category to my contact first i check if i really can add that category to my contact in base of a restriction model.

Let me give you an example... think in contact of a enterprise... supliers, customers, employees... if a contact have employees category he cant have a suplier category too


The model its something like this:

Categories
Id_category (PK)
CategoryName

Category_Parents
Id_CategoryParent (PK)
Id_Category (fk to Categories.Id_Category)
Id_Parent (fk to Categories.Id_Category)

Categories_Restrictions
Id_CategoryRestriction
Id_CategoryChoosed
Id_CategoryRestricted
Restriction (choice of Y or N)*
*if choose 'Y' means that your entity must already has added a id_categoryRestricted to permit choose id_categoryChoosed
*if choose 'N' means that if your entity has id_CategoryRestricted added you can permit add id_categoryChoosed

Relationships (or use choice of 'and', 'or, 'xor', etc)
Id_Relationship (PK)
RelationshipName
RelationshipDescription

Groups
Id_Groups (PK)
GroupName
Id_Relationship (fk to Relationships.Id_Relationship)

Categories_Groups
Id_CategoryGroup (PK)
Id_Category (fk to Categories.Id_Category)
Id_Group (fk to Groups.Id_Groups)

InterGroups_Relations
Id_InterGroupsRelations
InterGroupRelationName
Id_Group1 (fk to Groups.Id_Groups)
Id_Group2 (fk to Groups.Id_Groups)
Id_Relationship (fk to Relationships.Id_Relationship)

With this you can add categories, group them and make groups of groups. The elements of a group can have restrictions between them for example the categories of some group can have an xor relation, so only one of them can be choosed for a entity.
When you choose a Category for your entity first make a check if have some restritction if pass every test then you make add that category.

first check for Categories_Restrictions
then check for groups restrictions
then check for intergroups restrictions

its complicated, i know, also i know that can be better than that... its my little help :P

See ya (sorry my bad english too)



2013/8/21 Gerd Koetje <deonline...@gmail.com>
i would vote for the m2m model, but i think i loose it there a bit

--
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 http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
Message has been deleted

Gerd Koetje

unread,
Aug 21, 2013, 5:26:06 PM8/21/13
to django...@googlegroups.com
example of what im doing

<code>

from django.db import models

from django.contrib.auth.models import User

from django.contrib.sites.models import Site



# Create your models here.


       

# formulier keuze databases


class Kleurogen_data (models.Model):

    name = models.CharField(max_length=255)

    

    def __unicode__(self):

        return self.name


class Haarkleur_data (models.Model):

    name = models.CharField(max_length=255)

    

    def __unicode__(self):

        return self.name

    

class Lengtehaar_data (models.Model):

    name = models.CharField(max_length=255)

    

    def __unicode__(self):

        return self.name

    

class Landen (models.Model):

    slug = models.CharField(max_length=255)

    name = models.CharField(max_length=255)

    

    def __unicode__(self):

        return self.name


class Regio (models.Model):

    slug = models.CharField(max_length=255)

    name = models.CharField(max_length=255)

    land = models.ForeignKey(Landen)

    

    def __unicode__(self):

        return self.name


    

# sitedeals


class Sitedetails(models.Model):

    JANEE = (

    ('Y','Ja'),

    ('N','Nee'),              

    )

    

    TAAL = (

    ('Nederlands','Nederlands'),

    ('Engels','Engels'),              

    )

        

    site = models.ForeignKey(Site)

    pub_date = models.DateTimeField('date published')

    

    sitetekst = models.TextField()

    beschrijving = models.TextField()

    keywords = models.TextField()

    google = models.TextField()

    

    adult = models.CharField(max_length=2, choices=JANEE, default='N')

    sitetaal = models.CharField(max_length=2, choices=TAAL, default='Nederlands')

        

    dailxsid = models.CharField(max_length=255)   

    smtploc = models.CharField(max_length=255)

    smtpuser = models.CharField(max_length=255)

    smtppass = models.CharField(max_length=255)

    

    ontvangen = models.IntegerField(default=0)

    verzonden = models.IntegerField(default=0)

    aanmeldingen = models.IntegerField(default=0)

    afmeldingen = models.IntegerField(default=0)

    views = models.IntegerField(default=0)

    profielen = models.IntegerField(default=0)

    

    

    class Meta:

        unique_together = ["site"]

        verbose_name_plural = "Sitedetails"

        app_label = 'sites'

        

        def __unicode__(self):

            return self.keywords

      

      

      

class Profielen(models.Model):

    JANEE = (

    ('Y','Ja'),

    ('N','Nee'),              

    )

    

    GENDER = (

    ('Man','Man'),

    ('Vrouw','Vrouw'),              

    )

    

  

    

    profielnaam = models.CharField(max_length=255)

    leeftijd = models.CharField(max_length=3)

    

    issoort = models.CharField(max_length=10, choices=GENDER, default='Man')

    zoektsoort = models.CharField(max_length=10, choices=GENDER, default='Vrouw')

    adult = models.CharField(max_length=2, choices=JANEE, default='N')

    partner = models.CharField(max_length=2, choices=JANEE, default='N')

    

    regio = models.ForeignKey(Regio, default='Amsterdam')

    land = models.ForeignKey(Landen, default='Nederland')

    

    

    kleurogen = models.ManyToManyField(Kleurogen_data, blank=False, null=False)

    haarkleur = models.ManyToManyField(Haarkleur_data, blank=False, null=False)

    lengtehaar = models.ManyToManyField(Lengtehaar_data, blank=False, null=False)



    postuur = models.CharField(max_length=255)

    gewicht = models.CharField(max_length=255)

    afkomst = models.CharField(max_length=255)

    gebjaar = models.CharField(max_length=255)

    gebmaand = models.CharField(max_length=255)

    gebdag = models.CharField(max_length=255)

    lengte = models.CharField(max_length=255)

    burgelijkestaat = models.CharField(max_length=255)

    opleidingsniveau = models.CharField(max_length=255)

    levensstijl = models.CharField(max_length=255)

    ikvoelme = models.CharField(max_length=255)

    inter = models.CharField(max_length=255)

    haarstijl = models.CharField(max_length=255)

    relatie = models.CharField(max_length=255)

    cupmaat = models.CharField(max_length=255)

    geaardheid = models.CharField(max_length=255)

    sterrenbeeld = models.CharField(max_length=255)   

    uiterlijkekenmerken = models.TextField()

    belangstellingvoor = models.TextField()

    beschrijving = models.TextField()

    omschrijving = models.TextField()

    algemenevoorkeuren = models.TextField()

    sexvoorkeuren = models.TextField()

    bdsmvoorkeuren = models.TextField()

    sexkenmerken = models.TextField()

    belangstelling = models.TextField()

    omschrijvingengels = models.TextField()

    beschrijvingengels = models.TextField()

    likes = models.IntegerField(default=0)

    views = models.IntegerField(default=0)

    user = models.ForeignKey(User) 

     
     class Meta:

       verbose_name_plural = "Profielen"


    def __unicode__(self):
        return self.profielnaam


class
Favorieten(models.Model):

    user = models.ForeignKey(User)
    favoriet = models.ForeignKey(Profielen)
    pub_date = models.DateTimeField('date published')

   

</code>

Rafael E. Ferrero

unread,
Aug 21, 2013, 5:57:43 PM8/21/13
to django...@googlegroups.com


2013/8/21 Gerd Koetje <deonline...@gmail.com>

--
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 http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages