gpt4 book ai didi

Django - 默认外键

转载 作者:行者123 更新时间:2023-12-04 15:33:40 25 4
gpt4 key购买 nike

我在为模型中的 ForeignKey 字段设置默认值时遇到问题。这是我的代码:

class Group(models.Model):
name = models.CharField(max_length=50)
event = models.ForeignKey(Event)

def __str__(self):
return self.name

class Slot(models.Model):
title = models.CharField(max_length=50, blank = True)
name = models.ForeignKey(SlotName)
is_taken = models.BooleanField(default=False)
usr = models.ForeignKey('auth.User', blank=True, null=True)
group = models.ForeignKey(Group)
event = models.ForeignKey(Event) #i would like to have something like "default=group.event" here

def __str__(self):
return self.name.name

这是一个简单的日历应用程序,它的想法是我有一个人们可以在指定组中注册的事件。所以我希望能够在创建事件的同时创建一些组,然后向组添加插槽。

我在插槽模型中有事件字段,因为我需要确保人们无法同时注册 2 个插槽,并且我还需要计算插槽数量(事件模型中的字段)。我显然需要两个模型中的事件字段指向同一个事件,所以我想我可以为我的 Slot.event 设置一个默认值。

随意提交任何解决方法,我可以轻松地重新设计我的应用程序。预先感谢您的帮助。

编辑:
我会尽量更好地描述我的应用程序:它应该是一个在线游戏社区的日历(游戏是 arma3)。我希望每场事件的参加人数不超过 40 人,但对于我们的游戏 session ,我们需要等级制度。特别是我们把自己分成小组,每个小组都有不同的角色(队长、机枪手、步枪手等)。我想要一个日历,您可以在其中查看特定事件的详细信息,包括组和所有不同的时段,并注册一个特定的时段。我希望它能澄清它。请原谅所有错误,英语不是我的第一语言。

我之前忘了提到它,但我绝对是 Django 的初学者,并且正在 python 3.4 上运行 Django 1.7。使用 lambda 的解决方案似乎不起作用,这是错误日志(抱歉没有缩进):
(own)qwe@qwe:~/Projects/own/src$ python manage.py makemigrations
Migrations for 'cal':
0021_auto_20141202_1913.py:
- Remove field title from slot
- Alter field event on slot
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/qwe/Projects/own/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/qwe/Projects/own/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/qwe/Projects/own/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/qwe/Projects/own/lib/python3.4/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/home/qwe/Projects/own/lib/python3.4/site-packages/django/core/management/commands/makemigrations.py", line 124, in handle
self.write_migration_files(changes)
File "/home/qwe/Projects/own/lib/python3.4/site-packages/django/core/management/commands/makemigrations.py", line 152, in write_migration_files
migration_string = writer.as_string()
File "/home/qwe/Projects/own/lib/python3.4/site-packages/django/db/migrations/writer.py", line 129, in as_string
operation_string, operation_imports = OperationWriter(operation).serialize()
File "/home/qwe/Projects/own/lib/python3.4/site-packages/django/db/migrations/writer.py", line 86, in serialize
arg_string, arg_imports = MigrationWriter.serialize(arg_value)
File "/home/qwe/Projects/own/lib/python3.4/site-packages/django/db/migrations/writer.py", line 310, in serialize
return cls.serialize_deconstructed(path, args, kwargs)
File "/home/qwe/Projects/own/lib/python3.4/site-packages/django/db/migrations/writer.py", line 221, in serialize_deconstructed
arg_string, arg_imports = cls.serialize(arg)
File "/home/qwe/Projects/own/lib/python3.4/site-packages/django/db/migrations/writer.py", line 323, in serialize
raise ValueError("Cannot serialize function: lambda")
ValueError: Cannot serialize function: lambda

这是更新的代码(同样可能存在缩进错误,我在这里无法理解这个代码系统):
class Event(models.Model):
author = models.CharField(max_length=200)
title = models.CharField(max_length=200)
details = models.TextField(max_length=5000)
slots_no = models.PositiveIntegerField(default=0, editable=False)
created_date = models.DateTimeField(
auto_now=True)
event_date = models.DateField(
blank=True, null=True)
event_time = models.TimeField(default='20:00')
created_by = models.ForeignKey('auth.User')
type = models.ForeignKey(EventType)


def publish(self):
self.save()

def __str__(self):
return self.title

class Group(models.Model):
name = models.CharField(max_length=50)
event = models.ForeignKey(Event)

def __str__(self):
return self.name


class Slot(models.Model):
#title = models.CharField(max_length=50, blank = True)
name = models.ForeignKey(SlotName)
is_taken = models.BooleanField(default=False)
usr = models.ForeignKey('auth.User', blank=True, null=True)
group = models.ForeignKey(Group)
event = models.ForeignKey(Event)
def __str__(self):
return self.name.name

最佳答案

我在尝试将外键添加到没有默认值的迁移时遇到了类似的问题。删除 lambda 表达式并将默认值设为可空 (None),然后使用以下代码示例重试。它非常适合我。

例如:

from django.db import models                                                    


class Slot(models.Model):
event = models.ForeignKey('Event', null=True)
group = models.ForeignKey('Group', null=True)
def save(self, *args, **kwargs):
if(self.group is not None and self.group.event is not None):
self.event = self.group.event
super(Slot, self).save(*args, **kwargs)

class Event(models.Model):
name = models.CharField(max_length=100)

class Group(models.Model):
name = models.CharField(max_length=100)
event = models.ForeignKey('Event')

为简单起见,我省略了模型上不感兴趣的字段。我希望它能帮助你。

关于Django - 默认外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27251652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com