Django:固定长度的CharField,如何? [英] Django: CharField with fixed length, how?

查看:90
本文介绍了Django:固定长度的CharField,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望模型中具有固定长度的CharField。换句话说,我希望只有指定的长度才有效。

I wold like to have in my model a CharField with fixed length. In other words I want that only a specified length is valid.

我试图做类似的事情

volumenumber = models.CharField('Volume Number', max_length=4, min_length=4)

但它给我一个错误(似乎我可以同时使用max_length和min_length。)。

but it gives me an error (it seems that I can use both max_length and min_length at the same time).

还有另一种快速方法吗?

Is there another quick way?

谢谢

编辑:

一些人的建议,我会更具体一些:

Following the suggestions of some people I will be a bit more specific:

我的模型是这样的:

class Volume(models.Model):
    vid = models.AutoField(primary_key=True)
    jid = models.ForeignKey(Journals, db_column='jid', null=True, verbose_name = "Journal")
    volumenumber = models.CharField('Volume Number')
    date_publication = models.CharField('Date of Publication', max_length=6, blank=True)
    class Meta:
        db_table = u'volume'
        verbose_name = "Volume"
        ordering = ['jid', 'volumenumber']
        unique_together = ('jid', 'volumenumber')
    def __unicode__(self):
        return (str(self.jid) + ' - ' + str(self.volumenumber))

我做什么希望 volumenumber 必须正好是4个字符。

What I want is that the volumenumber must be exactly 4 characters.

IE
,如果有人插入'4b'django时出现错误,因为它期望包含4个字符的字符串。

I.E. if someone insert '4b' django gives an error because it expects a string of 4 characters.

所以我尝试了

volumenumber = models.CharField('Volume Number', max_length=4, min_length=4)

但是它给了我这个错误:

but it gives me this error:

Validating models...
Unhandled exception in thread started by <function inner_run at 0x70feb0>
Traceback (most recent call last):
  File "/Library/Python/2.5/site-packages/django/core/management/commands/runserver.py", line 48, in inner_run
    self.validate(display_num_errors=True)
  File "/Library/Python/2.5/site-packages/django/core/management/base.py", line 249, in validate
    num_errors = get_validation_errors(s, app)
  File "/Library/Python/2.5/site-packages/django/core/management/validation.py", line 28, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/Library/Python/2.5/site-packages/django/db/models/loading.py", line 131, in get_app_errors
    self._populate()
  File "/Library/Python/2.5/site-packages/django/db/models/loading.py", line 58, in _populate
    self.load_app(app_name, True)
  File "/Library/Python/2.5/site-packages/django/db/models/loading.py", line 74, in load_app
    models = import_module('.models', app_name)
  File "/Library/Python/2.5/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Users/Giovanni/src/djangoTestSite/../djangoTestSite/journaldb/models.py", line 120, in <module>
    class Volume(models.Model):
  File "/Users/Giovanni/src/djangoTestSite/../djangoTestSite/journaldb/models.py", line 123, in Volume
    volumenumber = models.CharField('Volume Number', max_length=4, min_length=4)
TypeError: __init__() got an unexpected keyword argument 'min_length'

如果我仅使用 max_length或 min_length,显然不会出现。

That obviously doesn't appear if I use only "max_length" OR "min_length".

我阅读了文档在django网站上,似乎我是对的(我不能同时使用两者),所以我在问是否还有另一种方法可以解决问题。

I read the documentation on the django web site and it seems that I'm right (I cannot use both together) so I'm asking if there is another way to solve the problem.

再次感谢

推荐答案

CharField数据库模型字段实例仅具有 max_length 参数,如 docs 中所述。这可能是因为SQL中只有一个最大字符长度约束条件。

CharField database model field instances only have a max_length parameter, as indicated in the docs. This is probably because there is only a max character length contraint equivalent in SQL.

表单字段CharField 对象确实具有 min_length 参数。因此,您必须为此特定模型编写一个自定义ModelForm,并用该自定义模型覆盖默认的管理模型形式。

Form Field CharField objects, on the other hand, do have a min_length parameter. So you'd have to write a custom ModelForm for this specific model and override the default admin model form with the custom one.

类似这样的事情:

# admin.py

from django import forms

...

class VolumeForm(forms.ModelForm):
    volumenumber = forms.CharField(max_length=4, min_length=4)

    class Meta:
        model = Volume


class VolumeAdmin(admin.ModelAdmin):
    form = VolumeForm

...

admin.site.register(Volume, VolumeAdmin)

这篇关于Django:固定长度的CharField,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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