Django模型选择 [英] Django Model Choices

查看:164
本文介绍了Django模型选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



到目前为止,我一直在模型中的批准字段中出现问题。我想要被批准为3个选择中的1个,但我看到的是所有三个选择的元组。在'./manage.py shell'中,我得到

 >>> list.objects.all()[0] .approved 
((u'1',u'Awaiting'),(u'2',u'No'),(u'3',u'Yes '))

我的模型:

 从django.db导入模型

#在这里创建您的模型。
类目录(models.Model):
name = models.CharField(max_length =50)

类列表(models.Model):
name = models.CharField(max_length =50)
directory = models.ForeignKey(directory)
birthday = models.DateField()
state = models.CharField(max_length =2)
owner = models.CharField(max_length =50)
approved =(
(u'1',u'Awaiting'),
(u'2',u'否'),
(u'3',u'Yes'),

另外一个问题是:但是每当我做模型更改,并尝试使用南方迁移模式,我的命令行将冻结,也不会完成迁移模式。任何可能的建议,为什么它冻结?它可以检测到变化,但不会完成它们的实现。因为它永远不会完成,当我点击模型进行更改时,我无法通过管理面板访问我的模型,我无法加载页面。

 我运行命令的顺序是
./manage.py convert_to_south myapp
./manage.py schemamigration southtut --auto
./manage.py migrate南通(在这几条线出现之后,这个命令从来没有进行过)


解决方案

批准,因为它不是一个字段,它只是一个包含三个选择的类属性。选择需要是实际字段的属性:

  APPROVAL_CHOICES =(
(u'1',u '等待'),
(u'2',u'No'),
(u'3',u'Yes'),

approved = models。 CharField(max_length = 1,choices = APPROVAL_CHOICES)


I've been stumped by how to make choices within my models for hours.

So far I've been having issues with my approved field in the model. I want approved to be 1 of the 3 choices,but what I appear to get is a tuple of all three choices. Within './manage.py shell', I get

>>> listing.objects.all()[0].approved
((u'1', u'Awaiting'), (u'2', u'No'), (u'3', u'Yes'))

My Model:

from django.db import models

# Create your models here.
class directory(models.Model):
    name = models.CharField(max_length="50")

class listing(models.Model):
    name = models.CharField(max_length="50")
    directory = models.ForeignKey(directory)
    birthday = models.DateField()
    state = models.CharField(max_length="2") 
    owner = models.CharField(max_length="50")
    approved = (
        (u'1', u'Awaiting'),
        (u'2', u'No'),
        (u'3', u'Yes'),
    )

Also side question: But whenever I make model changes and try to migrate schemas with South my commandline will freeze up and won't ever finish migrating schemas. Any possible suggestions for why it freezes? It can detect changes but wont ever finish implementing them. Because it never finishes, I cant access my model through the admin panel anymore when I click on the model to make changes, I can never load the page.

The order in which I run the commands are
    ./manage.py convert_to_south myapp
    ./manage.py schemamigration southtut --auto
    ./manage.py migrate southtut ( never progresses on this command after the first few lines appear)

解决方案

approved as you have it isn't a field, it's simply a class attribute containing the three choices. The choices need to be an attribute of an actual field:

APPROVAL_CHOICES = (
    (u'1', u'Awaiting'),
    (u'2', u'No'),
    (u'3', u'Yes'),
)
approved = models.CharField(max_length=1, choices=APPROVAL_CHOICES)

这篇关于Django模型选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆