26
有我的客戶模型的一部分:
class Guest(models.Model):
event = models.ForeignKey(Event, related_name='guests')
user = models.ForeignKey(User, unique=True, related_name='guests')
...
形式來從客戶的反應:
class RSVPForm(forms.Form):
attending_d= forms.ChoiceField(choices=VISIBLE_ATTENDING_CHOICES, initial='yes', widget=forms.RadioSelect)
attending_b = forms.ChoiceField(choices=VISIBLE_ATTENDING_CHOICES, initial='yes', widget=forms.RadioSelect)
number_of_guests = forms.IntegerField(initial=0)
comment = forms.CharField(max_length=255, required=False, widget=forms.Textarea)
....
def save(self):
guest = self.guest_class.objects.get(user=1)
guest.attending_status_d = self.cleaned_data['attending_d']
guest.attending_status_b = self.cleaned_data['attending_b']
guest.number_of_guests = self.cleaned_data['number_of_guests']
guest.comment = self.cleaned_data['comment']
guest.save()
return guest
問題在於保存方法。我如何將訪客與當前登錄的用戶相關聯?
guest = self.Guest.objects.get(user=1)
而不是user=1
我需要有當前登錄的用戶的ID。
謝謝!
照顧,你打破了里氏替換原則:https://en.wikipedia.org/wiki/Liskov_substitution_principle。看看這個答案添加用戶參數沒有打破利斯科夫原則:http://stackoverflow.com/a/7300076/801924 – bux 2016-05-19 12:16:43