Django model.foreignKey并返回self.text错误 [英] Django model.foreignKey and return self.text errors

查看:24
本文介绍了Django model.foreignKey并返回self.text错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在Django中处理model.py,并且出现2个pylint错误.我不明白为什么?这是pylint的问题还是我的代码做错了?

So I'm working on the model.py in Django and i'm getting 2 pylint errors. I don't understand why? is this an issue with pylint or something i'm doing wrong in my code.

E1120:No value for argument 'on_delete' in constructor call    
E1136:Value 'self.text' is unsubscriptable

  • 第一个在Entry的第19行topic = models.ForeignKey(Topic)

    • The first is on line 19, in Entry topic = models.ForeignKey(Topic)

      第二个是第24行self.text [:50]

      The second is on line 24 self.text[:50]

      如果我删除条目类,则代码有效

      If I remove the entry class the code works

      from django.db import models
      
      # Create your models here.
      class Topic(models.Model):
      """A topic the user is learning about"""
      text = models.CharField(max_length=200)
      date_added = models.DateTimeField(auto_now_add=True)
      
      def __str__(self):
          """Return a string representation of the model."""
          return self.text
      
      class Entry(models.Model):
      """Something specific learned about a topic"""
      topic = models.ForeignKey(Topic)
      text = models.TextField()
      date_added = models.DateTimeField(auto_now_add=True)
      
      class Meta:
          verbose_name_plural = "entries"
      
      def __str__(self):
          """Return a string representation of the model."""
          return self.text[:50] + "..."
      

      推荐答案

      问题是Django 1.9中的ForeignKey需要Django 2.0中的1个位置参数.

      The problem was ForeignKey in Django 1.9 required 1 positional argument in Django 2.0 ForeignKey 2 required positional argument

        topic = models.ForeignKey("Topic", on_delete=models.PROTECT)
      

      这篇关于Django model.foreignKey并返回self.text错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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