Django Models:使用DateField作为ForeignKey时出错 [英] Django Models: error when using DateField as ForeignKey

查看:536
本文介绍了Django Models:使用DateField作为ForeignKey时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将模型类的DateField用作另一个模型类的ForeignKey,并在两个类中使用默认设置为today时遇到问题。错误消息是:


django.core.exceptions.ValidationError:['self.date''的值具有无效的日期格式。 ]


code:

 类DailyImage(models.Model):
date = models.DateField(auto_now_add = True,unique = True)
name = models.CharField(max_length = 1000 )
image = models.CharField(max_length = 1000)
location = models.CharField(max_length = 1000)

def __str __(self):
return str( self.id)+:+ self.name +,+ self.location
$ b $ class JournalEntry(models.Model):
date = models.DateField(auto_now_add = True)
journal = models.CharField(max_length = 5000)
image = models.ForeignKey(DailyImage,to_field ='date',default ='self.date')

该网站是一个日常杂志。每天,它都会将一个来自unsplash.it的图像添加到DailyImage类中,DailyImage类将显示为主页上的标题,并在页面上显示当天创建的日记条目。创建日记帐分录时,应自动引用当天创建的图像。

在shell中测试它,date字段看起来是匹配的,但格式如下:datetime.date(YYYY,MM,DD)

 >>> a = JournalEntry.objects.get(pk = 1)
>>> a
< JournalEntry:test>
>>> a.date
datetime.date(2016,11,7)
>>>从journal.models导入DailyImage as image
>>> b = image.objects.get(pk = 1)
>>> b.date
datetime.date(2016,11,7)
>>> b.date == a.date
True

有关如何完成此操作的任何建议正确的将不胜感激!

解决方案

a.date返回datetime对象,但您必须设置格式。

  t = datetime.date(2016,11,7)
t.strftime(%Y-%m-%d )
#'2016-11-07'

您也可以设置默认日期时间format.py

  DATETIME_FORMAT ='Ym -d'

然而,我不确定这是否会成为您情况的解决方案。


Having an issue when trying to use DateField of a model class as the ForeignKey for another model class, and using default set to today on both classes. The error message is:

django.core.exceptions.ValidationError: ["'self.date' value has an invalid date format. It must be in YYYY-MM-DD format."]

code:

class DailyImage(models.Model):
    date = models.DateField(auto_now_add=True, unique=True)
    name = models.CharField(max_length = 1000)
    image = models.CharField(max_length = 1000)
    location = models.CharField(max_length = 1000)

    def __str__(self):
        return str(self.id) + ": " + self.name + ", " + self.location

class JournalEntry(models.Model):
    date = models.DateField(auto_now_add=True)
    journal = models.CharField(max_length = 5000)
    image = models.ForeignKey(DailyImage, to_field='date', default='self.date')

The site is a daily journal. Each day, it adds an image from unsplash.it to the DailyImage class, which is then displayed as the header on the home page, and header on the page for the journal entry created that day. When a journal entry is created, it should automatically be referenced to the image that was created that day.

testing it in shell, the date fields seem to match, but are formatted as: datetime.date(YYYY, MM, DD)

>>> a = JournalEntry.objects.get(pk=1)
>>> a
<JournalEntry: test>
>>> a.date
datetime.date(2016, 11, 7)
>>> from journal.models import DailyImage as image
>>> b = image.objects.get(pk=1)
>>> b.date
datetime.date(2016, 11, 7)
>>> b.date == a.date
True

Any suggestions to how this should be done properly would be greatly appreciated!

解决方案

a.date returns the datetime object, but you have to set the format.

t = datetime.date(2016, 11, 7) 
t.strftime("%Y-%m-%d")
# '2016-11-07'

You could also set the default datetime format in settings.py

DATETIME_FORMAT = 'Y-m-d'

However I'm not sure that would be a solution in your situation.

这篇关于Django Models:使用DateField作为ForeignKey时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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