Python 是否可以将datetime列添加到多个字段中?

Python 是否可以将datetime列添加到多个字段中?,python,django,postgresql,datetime,django-models,Python,Django,Postgresql,Datetime,Django Models,我想知道是否有办法将datetime auto\u now\u add=True列添加到多个字段中。 我有下面的型号 class Photographer(models.Model): user = models.OneToOneField(User, primary_key=True) bio = models.CharField(max_length=140, blank=True) favorites = models.ManyToManyField('Pho

我想知道是否有办法将datetime auto\u now\u add=True列添加到多个字段中。 我有下面的型号

class Photographer(models.Model):
     user = models.OneToOneField(User, primary_key=True)
     bio = models.CharField(max_length=140, blank=True)
     favorites = models.ManyToManyField('Photo', through='Favorites', related_name = 'photographer_favs')
     likes = models.ManyToManyField('Photo', related_name='likedby', blank=True)
在我的postgresql数据库中,我有一个单独的“djangoapp_摄影师_likes”表

在此表中,我有三列:

id                 photographer_id        photo_id
[PK] serial        integer                integer
    1                  3                    6
    2                  3                    7
    3                  3                    8
在投入生产几个月后,我意识到我需要这些我们喜欢的照片的日期和时间。因此,我没有三列,而是有四列:

    id             photographer_id        photo_id      liked
[PK] serial        integer                integer       timestamp with time zone
    1                  3                    6           2013-07-10 23:30:19.559463+00
    2                  3                    7           2013-07-11 17:00:53.39614+00
    3                  3                    8           2013-07-11 17:25:28.4953+00
即使不在模型中,是否可以添加此额外列?我想要它,这样每当一张图片被喜欢时,它被喜欢的时间就会被记录下来。如果这是不可能的,我还应该研究什么其他的解决办法

编辑

这是我的照片模型:

class Photo(models.Model):
     title = models.CharField(max_length=40, blank=True)
     photographer = models.ForeignKey(Photographer, related_name = 'shot_owner')
     postTime = models.DateTimeField(auto_now_add=True)
     description = models.CharField(max_length=255, blank=True)
     url = models.CharField(max_length=128)

是的,这很正常。您将为Likes关系声明一个显式模型,并在其上添加datetime字段。查看完整的详细信息和一些示例-组成员资格示例包括一个“加入日期”字段,该字段与您喜欢的时间戳的想法相同。在字段定义中使用
auto\u now\u add=True
,可以自动记录时间戳

class Photographer(models.Model):
    # as above, but:
    likes = models.ManyToManyField('Photo', through='PhotoLikes', related_name='likedby', blank=True)

class PhotoLikes(models.Model):
    class Meta:
        db_table = 'djangoapp_photographer_likes'
        # or whatever your table is called
    photo = models.ForeignKey(Photo)
    photographer = models.ForeignKey(Photographer)
    liked = models.DateTimeField(null=True, blank=True, auto_now_add=True)
正如文档所指出的,在这种设置中,创建like的方法受到了更多的限制:

与普通的多对多字段不同,您不能使用添加、创建或分配(即beatles.members=[…])来创建关系

为什么??您不能只在个人和组之间创建关系-您需要为成员模型所需的关系指定所有细节。简单的add、create和assignment调用不提供指定此额外细节的方法。因此,对于使用中间模型的多对多关系,它们被禁用。创建此类关系的唯一方法是创建中间模型的实例


在这种情况下,理论上可以确定额外的细节,因为这是一个自动时间戳,但Django无法检测到。

关系表(解析两个实体表之间的多对多关系)具有属性是非常常见的。我们只是想确保属性是关系的属性,而不是其中一个实体的属性,或者它本身不是实体。感谢您的快速回复,即使“喜欢”已经是很多照片的外国钥匙,我会遇到问题吗?还有!我相信它是自动的。\u now\u add:)您需要确保模型使用您现有的表,可能通过重命名现有表或使用中介模型的
Meta
类上的
db\u table
设置。或者选择合适的中介模型名称。当然,您必须自己添加数据库列—syncdb不会这样做,因为它将位于现有表中。谢谢。我有Django South,它将处理列添加。我已经编辑了我的问题,并在其中添加了我的照片模型以供澄清。如果可以的话,你能给我一个更明确的答案,这样我就可以接受并投票支持你的答案吗?