每次创建医院时我都会喜欢这样,同样创建相应的用户帐户,允许新创建的帐户登录....
models.py
class Hospital(models.Model):
"""
Model representing individual Hospitals
"""
id = models.UUIDField(primary_key=True, unique=True, db_index=True, default=uuid.uuid4, editable=False)
hospital_name = models.CharField(help_text="full name of hospital", max_length=200)
slug = models.SlugField(max_length=200)
hospital_logo = models.ImageField(upload_to='hospital_logo',)
hospital_address = models.CharField(max_length=500)
hospital_email = models.EmailField(max_length=254)
hospital_website = models.CharField(max_length=200)
hospital_rc_number = models.PositiveSmallIntegerField()
date_added = models.DateField(auto_now=True)
forms.py
class HospitalForm(forms.ModelForm):
"""
Forms for Hospital creation
"""
class Meta:
model = Hospital
fields = ('hospital_name', 'hospital_address', 'hospital_email', 'hospital_website', 'hospital_rc_number','hospital_logo')
widgets = {
'hospital_name': forms.TextInput(attrs={'class': 'form-control'}),
'hospital_address': forms.TextInput(attrs={'class': 'form-control'}),
'hospital_email': forms.TextInput(attrs={'class': 'form-control'}),
'hospital_website': forms.TextInput(attrs={'class': 'form-control'}),
'hospital_rc_number': forms.TextInput(attrs={'class': 'form-control'}),
}
def save(self):
instance = super(HospitalForm, self).save(commit=False)
instance.slug = slugify(instance.hospital_name)
instance.save()
return instance
views.py
class HospitalCreate(CreateView):
model = Hospital
form_class = HospitalForm
template_name = 'hospital/hospital_add.html'
pk_url_kwarg = 'hospital_id'
success_url = reverse_lazy('hospital_list')
user_form = UserForm
答案 0 :(得分:1)
我不知道这是否是解决这个问题的最好办法,但它似乎正在做我想要的事情。我还没有完全检查是否会弹出其他问题。
我在医院模型中创建了与User类中的字段相对应的更多字段,并且我能够使用object.create_user
方法在User类中创建医院模型的实例。
models.py
class Hospital(models.Model):
"""
Model representing individual Hospitals
"""
id = models.UUIDField(primary_key=True, unique=True, db_index=True, default=uuid.uuid4, editable=False)
admin_username = models.CharField(unique=True, max_length=15)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
password = models.CharField(max_length=50)
password2 = models.CharField(max_length=50)
user = models.IntegerField()
hospital_name = models.CharField(help_text="full name of hospital", max_length=200)
slug = models.SlugField(max_length=200)
hospital_logo = models.ImageField(upload_to='hospital_logo',)
hospital_address = models.CharField(max_length=500)
hospital_email = models.EmailField(max_length=254)
hospital_website = models.CharField(max_length=200)
hospital_rc_number = models.PositiveSmallIntegerField()
date_added = models.DateField(auto_now=True)
forms.py
class HospitalForm(forms.ModelForm):
"""
Forms for Hospital creation
"""
class Meta:
model = Hospital
fields = ('hospital_name', 'hospital_address', 'hospital_email', 'hospital_website', 'hospital_rc_number',
'first_name','last_name','hospital_logo','admin_username','password','password2')
widgets = {
'admin_username': forms.TextInput(attrs={'class': 'form-control'}),
'first_name': forms.TextInput(attrs={'class': 'form-control'}),
'last_name': forms.TextInput(attrs={'class': 'form-control'}),
'password': forms.PasswordInput(attrs={'class': 'form-control'}),
'password2': forms.PasswordInput(attrs={'class': 'form-control'}),
'hospital_name': forms.TextInput(attrs={'class': 'form-control'}),
'hospital_address': forms.TextInput(attrs={'class': 'form-control'}),
'hospital_email': forms.TextInput(attrs={'class': 'form-control'}),
'hospital_website': forms.TextInput(attrs={'class': 'form-control'}),
'hospital_rc_number': forms.TextInput(attrs={'class': 'form-control'}),
}
def save(self):
instance = super(HospitalForm, self).save(commit=False)
instance.slug = slugify(instance.hospital_name)
instance.save()
return instance
和Views.py
class HospitalCreate(CreateView):
model = Hospital
form_class = HospitalForm
template_name = 'hospital/hospital_add.html'
pk_url_kwarg = 'hospital_id'
success_url = reverse_lazy('hospital_list')
def form_valid(self, form):
username = form.instance.admin_username
password = form.instance.password
email = form.instance.hospital_email
first_name = form.instance.first_name
last_name = form.instance.last_name
user_profile = User.objects.create_user(username=username, password=password, email=email,first_name=first_name,
last_name=last_name, is_staff='0', is_active='1')
user_profile.save()
form.instance.user = int(user_profile.id)
return super(HospitalCreate, self).form_valid(form)
答案 1 :(得分:0)
一种选择是在您的观看次数form_valid
method
class HospitalCreate(CreateView):
def form_valid(self, form):
form.save()
# create user here
return HttpResponseRedirect(self.get_success_url())