0

在模型中:

class Author(models.Model):
    author_name = models.CharField(max_length=50)
    email = models.CharField(max_length=100)
    editor =  ProfiledUserField(User)

    def __str__(self):
        return '%s'%(self.author_name)

自定义 ProfiledUserField

from django.db import models
from django.contrib.auth.models import User
from django.db.models.fields.related import ForeignKey
from django.forms.fields import Select, TextInput
import datetime

class ProfiledUser(object):
    def __init__(self,user_id):
        self.user_id = user_id

    def get_user(self):
        return self.user

    def create_user(self,username,password="user123"):
        user = User.objects.create(username=username,password=password)
        return user


class ProfiledUserField(ForeignKey):
    description = "a custom user field"
    __metaclass__ = models.SubfieldBase

    def __init__(self,*args,**kwargs):
        return super(ProfiledUserField,self).__init__(*args,**kwargs)

    def to_python(self,value):
        return value


    def get_prep_value(self,value):
        print 'in get prep value',datetime.datetime.now().strftime("%A, %d. %B %Y %I:%M:%f")
        return value.id

    def get_db_prep_value(self,value,connection,prepared=False):
        print 'in get Db prep value',datetime.datetime.now().strftime("%A, %d. %B %Y %I:%M:%f")
        return value.id

除了注册模型外,管理部分没有任何内容。

当我尝试通过管理员保存对象时,to_python 被调用两次,第一次是从选择小部件中选择用户对象,第二次使用值无,因此虽然我已经选择/提供了小部件的输入,但它第二次不存在 to_python 是调用导致验证错误“字段不能留空”(因为默认情况下编辑器不能为空或空白)

当我尝试通过 python shell 保存它时,

>>> author= Author.objects.create(author_name="Khalil Gibran" , email="kh@gmail.com", editor=user)
AttributeError: 'Author' object has no attribute 'editor_id'

>>> Author
<class 'articles.models.Author'>
>>> Author.__dict__
dict_proxy({'__module__': 'articles.models', 'MultipleObjectsReturned': <class 'articles.models.MultipleObjectsReturned'>, '_meta': <Options for Author>, '__str__': <function __str__ at 0x97d0d4c>, 'article_set': <django.db.models.fields.related.ForeignRelatedObjectsDescriptor object at 0x97e0b0c>, '_base_manager': <django.db.models.manager.Manager object at 0x97e02ec>, 'objects': <django.db.models.manager.ManagerDescriptor object at 0x97e034c>, 'editor': <django.db.models.fields.subclassing.Creator object at 0x97e024c>, 'DoesNotExist': <class 'articles.models.DoesNotExist'>, '__doc__': 'Author(id, author_name, email, editor_id)', '_default_manager': <django.db.models.manager.Manager object at 0x97e02ec>})
>>> dir(Author)
['DoesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__metaclass__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_base_manager', '_default_manager', '_deferred', '_get_FIELD_display', '_get_next_or_previous_by_FIELD', '_get_next_or_previous_in_order', '_get_pk_val', '_get_unique_checks', '_meta', '_perform_date_checks', '_perform_unique_checks', '_set_pk_val', 'article_set', 'clean', 'clean_fields', 'date_error_message', 'delete', 'editor', 'full_clean', 'objects', 'pk', 'prepare_database_save', 'save', 'save_base', 'serializable_value', 'unique_error_message', 'validate_unique']

ysql 外壳:

mysql> describe articles_author;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| author_name | varchar(50)  | NO   |     | NULL    |                |
| email       | varchar(100) | NO   |     | NULL    |                |
| editor_id   | int(11)      | NO   | MUL | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

我究竟做错了什么?

如果我在模型中有 null=True,blank=True,相同的代码工作得很好,但否则我无法保存它。如果我再次删除元类=Subfieldbase,一切正常。什么是解释?

4

0 回答 0