0
我有三個django模型(user
,member (extended user)
,subscription
),並與一個ForeignKey連接。保存數據到Django數據庫
然後當用戶想創建一個新帳戶,我把它保存到數據庫中,如下所示:
language = Language.objects.get(key="EN")
country = Country.objects.get(key="BE")
user = User()
user.username = form.cleaned_data['email']
user.first_name = form.cleaned_data['first_name']
user.last_name = form.cleaned_data['last_name']
user.email = form.cleaned_data['email']
user.set_password(form.cleaned_data['password'])
user.save()
member = Member()
member.number = form.cleaned_data['member_id']
member.name = '{} {}'.format(form.cleaned_data['firstname'], form.cleaned_data['lastname'])
member.address = form.cleaned_data['address']
member.postcode = form.cleaned_data['postcode']
member.city = form.cleaned_data['city']
member.country = country
member.telephone = form.cleaned_data['telephone']
member.mobile = form.cleaned_data['mobile']
member.user = user
member.language = language
member.active = False
member.save()
subscription = Subscription()
subscription.started = datetime.date.today()
subscription.type = Type.objects.get(default=True)
subscription.member = member
subscription.save()
這工作正常。
但我想問問是否有更好的方法將其存儲到數據庫?