小编ghi*_*ion的帖子

Django DateTimeField()和timezone.now()

好吧,当我运行功能测试时,奇怪的时区问题.Django 1.4,Python 2.7.在MySQL上的DateTimeField()中是否会截断毫秒数?这是我唯一的理论.

模型文件

from django.db import models
from django.utils import timezone

class Search(models.Model):
    query = models.CharField(max_length=200, null=True)
    query_date = models.DateTimeField(null=True)
Run Code Online (Sandbox Code Playgroud)

test.py

from django.test import TestCase
from django.utils import timezone
from search.models import Search

class SearchModelTest(TestCase):
def test_creating_a_new_search_and_saving_it_to_the_database(self):
    # start by creating a new Poll object with its "question" set
    search = Search()
    search.query = "Test"
    search.query_date = timezone.now()

    # check we can save it to the database
    search.save()

    # now check we can find it in the database again …
Run Code Online (Sandbox Code Playgroud)

python django django-models

7
推荐指数
2
解决办法
6299
查看次数

unique_together 外键对象属性

我有两个模型:Common 和 ARecord。ARecord 与 Common 具有外键关系。我想确保 ARecord 是独一无二的,它结合了 ARecord 和 Common 中的项目。

class Common(models.Model):
    NAIC_number = models.CharField(max_length=5)
    file_location_state = models.CharField(max_length=2)
    file_location_code = models.CharField(max_length=2)

class ARecord(models.Model):
    common = models.ForeignKey(Common)
    coverage_code = models.CharField(max_length=6)
    record_type = models.CharField(max_length=1)

    class Meta:
        unique_together = ('coverage_code', 'common__NAIC_number')
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试通过通常的双下划线访问外键对象属性时,我收到模型验证错误。

`arecord.arecord: "unique_together" refers to common__NAIC_number, a field that doesn't exist. Check your syntax.`
Run Code Online (Sandbox Code Playgroud)

这似乎应该是可能的,并且提出了一个略有不同的问题表明它是,但也许我错过了一些明显的东西?

django django-models

5
推荐指数
1
解决办法
4826
查看次数

标签 统计

django ×2

django-models ×2

python ×1