我已经实现了一个表单,我需要填充User对象中的字段(firstname,lastname,email)以及填写新的成员资格对象。我已经使用基于功能的视图(FBV)实现了这一点,但我觉得我应该能够使用基于类的视图(CBV)来实现这一点。问题的核心似乎是在不传入用户对象的情况下引用表单中的当前用户。在FBV中很容易做到,但我找不到任何使用CBV的例子。我想我必须在这里遗漏一些东西。
这是我的代码
models.py
class Membership(models.Model):
"""Represents an active membership of a user. Both the start_date and
end_date parameters are inclusive."""
DEFAULT_DURATION = 365 # the default number of days a membership is active
start_date = models.DateField(auto_created=True)
end_date = models.DateField(null=True)
membership_type = models.ForeignKey(MembershipType)
referral = models.ForeignKey(User, related_name='membership_referral', null=True)
# Contact Info
phone = PhoneNumberField()
# Address Fields
address_1 = models.CharField(max_length=255)
address_2 = models.CharField(max_length=255, blank=True)
city = models.CharField(max_length=64)
state = USStateField()
zip_code = USPostalCodeField()
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
"""Overload the save function to set the start and end date."""
self.start_date = datetime.date.today()
self.end_date = (self.start_date +
datetime.timedelta(days=self.membership_type.period))
super().save()
@property
def is_active(self):
return self.end_date >= datetime.date.today()
forms.py
class MembershipForm(ModelForm):
"""The Form shown to users when enrolling or renewing for membership."""
def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user", None)
_fields = ('first_name', 'last_name', 'email',)
_initial = model_to_dict(self.user, _fields) if self.user is not None else {}
super(MembershipForm, self).__init__(initial=_initial, *args, **kwargs)
self.fields.update(fields_for_model(User, _fields))
self.fields['referral'].required = False
class Meta:
model = Membership
fields = ['membership_type', 'referral', 'phone', 'address_1',
'address_2', 'city', 'state']
zip_code = USZipCodeField(max_length=5, required=True)
def save(self, *args, **kwargs):
self.user.first_name = self.cleaned_data['first_name']
self.user.last_name = self.cleaned_data['last_name']
self.user.email = self.cleaned_data['email']
self.user.save()
profile = super(MembershipForm, self).save(*args, **kwargs)
return profile
views.py
@login_required
def enroll(request):
template_name = 'enroll.html'
if request.method == 'POST':
form = MembershipForm(request.POST, user=request.user)
if form.is_valid():
form.save()
return redirect('/')
else:
form = MembershipForm(user=request.user)
return render(request, template_name, {'form': form})
答案 0 :(得分:3)
您可以通过self.request.user在基于类的视图中访问当前用户。它可以通过重新定义这样的验证方法在FormView中设置:
class YourView(CreateView)
...
def form_valid(self, form):
form.instance.user = self.request.user
return super(YourView, self).form_valid(form)
我在示例中使用了CreateView而不是FormView,因为对于编辑,出于安全目的,您应该另外检查当前实例的用户。
答案 1 :(得分:0)
虽然您的问题提到了CBV,但在代码中您使用的是FBV。
在FBV中,您可以访问要传递的请求变量。在这种情况下,您可以使用request.user。
对于CBV,Django允许您以self.request的形式访问请求对象。在默认' django.views.generic.base.View'的实施中CBV的一类,他们这样做是第一件事。
在此代码中检查def视图的第4行作为as_view的一部分 - https://github.com/django/django/blob/master/django/views/generic/base.py
所有对象(包括用户作为请求的一部分)都可以作为self.request.user访问。