gpt4 book ai didi

Django - 内联编辑多对多

转载 作者:行者123 更新时间:2023-12-04 03:05:51 26 4
gpt4 key购买 nike

我有以下型号

class Day(models.Model):
date = models.DateField(auto_now=False, auto_now_add=False)
price = models.FloatField()
payment_method = models.CharField(max_length = 200, blank=True)
payment_date = models.CharField(max_length=200, blank=True)
room = models.ForeignKey(Room, null=True, blank=True, verbose_name='Номер', on_delete=models.CASCADE)

def __unicode__(self):
return str(self.date)

class Reservation(models.Model):
start = models.DateField(verbose_name='Заезд', auto_now=False, auto_now_add=False, blank=False)
end = models.DateField(verbose_name='Выезд', auto_now=False, auto_now_add=False, blank=False)
check_in_time = models.TimeField(verbose_name='Время заезда', blank=False)
check_out_time = models.TimeField(verbose_name='Время выезда', blank=False)
has_refund = models.BooleanField(verbose_name='Возвратная бронь', default=True)
payed = models.BooleanField(verbose_name='Оплачено', default=False)
reserved_days = models.ManyToManyField(Day, blank=False)
additional_services = models.ManyToManyField(AdditionalService)
guest_name = models.CharField(verbose_name='Имя гостя', max_length=200, blank=True)
reservation_number = models.CharField(verbose_name='Номер брони', max_length=200, blank=True)

我想要的是能够编辑 DayReservation

我在 django 文档中尝试以下操作
class ReservedDaysInline(admin.TabularInline):
model = Reservation
extra = 1

class ReservationAdmin(admin.ModelAdmin):
inlines = (ReservedDaysInline,)

class DayAdmin(admin.ModelAdmin):
inline = (ReservedDaysInline,)


admin.site.register(Reservation, ReservationAdmin)
admin.site.register(Day, DayAdmin)

但它不起作用。

我究竟做错了什么 ?

最佳答案

doc 试试这个

class ReservedDaysInline(admin.TabularInline):
model = Reservation.reserved_days.through
extra = 1

class ReservationAdmin(admin.ModelAdmin):
inlines = (ReservedDaysInline,)
exclude = ('reserved_days',)

关于Django - 内联编辑多对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44584683/

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