Django,管理面板的唯一时间间隔

发布于 2024-08-21 06:28:13 字数 281 浏览 6 评论 0原文

问候, 假设我有这样的模型:

class Foo(models.Model):
    type = models.ForeignKey(Type)
    start_time = models.DateTimeField()
    end_time models.DateTimeField()

对于具有相同类型的每个 Foo 对象,我需要这个时间间隔(end_time - start_time)是唯一的,以便不可能创建具有冲突间隔的第二个 Foo。如何才能实现这一目标?

Greetings,
Assume I have such model:

class Foo(models.Model):
    type = models.ForeignKey(Type)
    start_time = models.DateTimeField()
    end_time models.DateTimeField()

For each Foo object that is having the same type, I need this time interval (end_time - start_time) to be unique so that creation of a second Foo with a clashing interval won't be possible. How can this be achieved ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殊姿 2024-08-28 06:28:13

请参阅有关 自定义 的文档管理界面中的验证

基本上,您必须创建自己的(模型)表单,例如 CustomFooAdminForm 并将其分配给管理模型:

class FooAdmin(admin.ModelAdmin):
    form = CustomFooAdminForm

在表单中您可以拥有类似的内容(请参阅表单中的自定义验证):

# more or less pseudo code
class CustomFooAdminForm(forms.ModelForm):

    def clean(self):
        cleaned_data = super(CustomFooAdminForm, self).clean()
        interval = cleaned_data.get("end_time") - cleaned_data.get("start_time")
        type = cleaned_data.get("type")

        q = Foo.objects.extra(select={'interval':'time_end - time_start'}
        counter = q.filter(interval=intervak, type=type).count()

        if counter > 0:
            raise forms.ValidationError("ERROR!!!!")

        # Always return the full collection of cleaned data.
        return cleaned_data

也许您必须转换 DateTimeField 转换为 UNIX 时间戳,然后才能在 SQL 中减去它们(UNIX_TIMESTAMP(time_end) - UNIX_TIMESTAMP (time_start) for MySQL)。或者您可以使用 DATEDIFF MySQL 中的 () 来获取差异。但请注意,如果您使用此类特殊函数(只要它们在同名的其他数据库中不可用),则将您的应用程序绑定到某个数据库。

See the documentation about custom validation in the admin interface.

Basically you have to create your own (model) form, lets say CustomFooAdminForm and assign it to the admin model:

class FooAdmin(admin.ModelAdmin):
    form = CustomFooAdminForm

and in the form you can have something like (see custom validation in forms):

# more or less pseudo code
class CustomFooAdminForm(forms.ModelForm):

    def clean(self):
        cleaned_data = super(CustomFooAdminForm, self).clean()
        interval = cleaned_data.get("end_time") - cleaned_data.get("start_time")
        type = cleaned_data.get("type")

        q = Foo.objects.extra(select={'interval':'time_end - time_start'}
        counter = q.filter(interval=intervak, type=type).count()

        if counter > 0:
            raise forms.ValidationError("ERROR!!!!")

        # Always return the full collection of cleaned data.
        return cleaned_data

Maybe you have to transform the DateTimeFields to UNIX timestamps, before you can subtract them in SQL (UNIX_TIMESTAMP(time_end) - UNIX_TIMESTAMP (time_start) for MySQL). Or you can use DATEDIFF() in MySQL to get the difference. But note that you tie your application to a certain database if you use such special functions (as long as they are not available in other databases under the same name).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文