django中的“自动完成”字段

时间:2018-10-06 18:09:36

标签: django django-models autocomplete

这是我的模特

class TimeSlots(models.Model):
start = models.TimeField(null=True, blank=True)
end = models.TimeField(null=True, blank=True)

class Meta:
    ordering = ['start']

def __str__(self):
    return '%s - %s' % (self.start, self.end)


class Event(models.Model):
event_date = models.DateField(null=False, blank=True)
start = models.OneToOneField(TimeSlots)
end = models.TimeField(null=True, blank=True)
available = models.BooleanField(default=True)
patient_name = models.CharField(max_length=60, null=True, blank=True)
phone_number = PhoneNumberField(blank=True, null=True)
stripePaymentId = models.CharField(max_length=150, null=True, blank=True)
stripePaid = models.BooleanField(null=False, blank=True, default=True)
key = models.UUIDField(primary_key=False, default=uuid.uuid4, 
editable=False)
sites = models.ManyToManyField(Site, null=True, blank=True)

class Meta:
    verbose_name = u'Scheduling'
    verbose_name_plural = u'Scheduling'

def __unicode__(self):
    return self.start

def get_absolute_url(self):
    url = reverse('admin:%s_%s_change' % (self._meta.app_label, self._meta.model_name), args=[self.pk])
    return u'<a href="%s">%s</a>' % (url, str(self.start))

我想要的是事件模型的最终值应该由选定的时间段自动填充 就像我从时隙中选择事件模型的开始值时,应该自动填充结束值

1 个答案:

答案 0 :(得分:0)

好吧,我在模型中添加了一个干净函数来解决了这个问题

    def clean(self):
    self.end = self.start.end

就这么简单