建数据表出现TypeError: getattr(): attribute name must be string错误

来源:1-1 强力django+杀手级xadmin 打造上线标准的在线教育平台

慕容1479236

2021-09-12

图片描述

from django.db import models
from apps.users.models import UserProfILE # 导入用户表
# Create your models here.
from django.core.exceptions import ValidationError
import datetime
class Category(models.Model):
    '''
    板块
    '''
    name = models.CharField(max_length=50,unique=True,null=False,blank=False,verbose_name='板块名称')
    brief = models.CharField(max_length=255, null=True, blank=True, verbose_name='简介')
    admin =models.ManyToManyField(UserProfILE,blank=True,verbose_name='版主')
    def __unicode__(self):
        return self.name
    class Meta:
        verbose_name = '板块'
        verbose_name_plural = verbose_name
        db_table = 'Category'
class Article(models.Model):
    '''
    帖子表
    '''
    title = models.CharField(max_length=255,null=False,verbose_name='标题')
    brief = models.CharField(max_length=255,null=True,blank=True,verbose_name='简介')
    category = models.ForeignKey(Category,on_delete=models.CASCADE,null=False,verbose_name='板块')
    content = models.TextField(verbose_name=u'文章简介')
    author = models.ForeignKey(UserProfILE,on_delete=models.CASCADE,null=False,verbose_name='作者')
    pub_data = models.DateTimeField(verbose_name='添加时间',null=True)
    last_modify = models.DateTimeField(auto_now=True,verbose_name='修改时间')
    priority = models.IntegerField(u'优先级',default=1000)
    status_choices = (('draft','草稿'),
                      ('published',u'已发布'),
                      ('hidden',u'隐藏'))
    status = models.CharField(choices=status_choices,default='published',verbose_name='文章状态')
    def __unicode__(self):
        return self.title
    def clean(self):
        '''用于判断文章状态'''
        if self.status =='draft' and self.pub_data is not None:
            raise ValidationError(('dd'))
        if self.status =='published' and self.pub_data is None:
            self.pub_data = datetime.date.today()
    class Meta:
        verbose_name = '文章'
        verbose_name_plural = verbose_name
        db_table = 'Article'



class Comment(models.Model):
    '''
    评论表
    '''
    article = models.ForeignKey(Article,on_delete=models.CASCADE,null=False,blank=False,verbose_name='所属文章')
    parent_comment = models.ManyToManyField('self',related_name='my_children',null=True,blank=False)
    comment_choices=((1,u'评论'),
                     (2,u'点赞'),)
    comment_type = models.IntegerField(choices=comment_choices,default=1)
    user = models.ForeignKey(UserProfILE,on_delete=models.CharField)
    comment_time = models.DateTimeField(auto_now_add=True)
    comment = models.TextField(blank=True,null=True)
    def clean(self):
        if self.comment_type ==1 and self.comment is None:
            raise ValidationError(('评论类容不能为空'))
    def __unicode__(self):
        return self.comment_type
    class Meta:
        verbose_name = '评论'
        verbose_name_plural = verbose_name
        db_table = 'Comment'

写回答

1回答

bobby

2021-09-13

你的django版本是多少? 最好将完整的错误站信息贴出来看看

0
0

Django+ xadmin 开发在线教育网站

一套通用的技术组合拳,助你解决大部分Python类网站后端问题

1911 学习 · 2149 问题

查看课程