2017-10-29 126 views
0

抱歉,如果標題不是那麼具有描述性,我不知道該如何表達。 我有兩個表格: 1.-包含代碼字段和另一個「is_taken」字段的「Matricula」表格處於「False」狀態。2.「User」表格和註冊新用戶時必須插入如果代碼存在,然後「is_taken」將是「真」,並允許用戶註冊,我的問題是......我該怎麼做?,哪種方式將是最好的?Django:如何使用「code」和pre_save信號註冊用戶

我正在處理「pre_save」信號,但沒有成功。

models.py

class Matricula(models.Model): 
    code = models.CharField(max_length=9, blank=True) 
    is_taken = models.BooleanField("¿Esta ocupado?", default=False) 
    created_at = models.DateTimeField("Fecha de creación", auto_now_add = True) 
    updated_at = models.DateTimeField("Fecha de actualización", auto_now = True) 

def __str__(self): 
    return "%s" %(self.matricula) 

class User(auth_models.AbstractBaseUser, auth_models.PermissionsMixin): 
    code = models.CharField("Matricula", max_length=9, blank=True) 
    email = models.CharField("Correo Electrónico", max_length=100, blank=True, unique=True) 
    first_name = models.CharField("Nombre(s)", max_length=60, blank=True) 
    last_name = models.CharField("Apellidos", max_length=60, blank=True) 
    is_staff = models.BooleanField("¿Es Staff?", default=False, blank=True) 
    is_active = models.BooleanField("¿Es Activo?", default=False, blank=True) 
    date_joined = models.DateTimeField("Fecha de registro", auto_now_add=True) 

或可能是在視圖中添加?在用戶註冊過程中?

而對於對不起我的英文不好

回答

1

儘量不要使用信號儘可能的,因爲它們通過控制反轉傷害代碼readabilty。

正如我理解您的問題,您不希望沒有邀請碼的人「Matricula」能夠在您的網站上註冊。你最好在你的報名表上填寫。也許這樣的事情:

class UserRegistrationForm(AuthRegistrationForm): 
    code = forms.CharField(max_length=9) 

    def clean(self): 
     cleaned_data = super(ContactForm, self).clean() 
     try: 
      matricula = Matricula.objects.get(code=cleaned_data.get('code', None), is_taken=False) 
     except ObjectDoesNotExist: 
      raise forms.ValidationError('Invalid code') 
     else: 
      cleaned_data['code'] = matricula 

     return cleaned_data 



# in your views: 

.... if form.is_valid(): 
     matricula = form.cleaned_data['code'] 
     matricula.is_taken = True 
     matricula.save() 
     new_user = form.save() 
..... 
+0

感謝您的回答,現在我想試試這個......我需要這個作品儘快 –

1

如果你想添加一個matricula爲每個用戶,你應該使用的關係,而不是一個CharField,假設每個User只有一個matricula使用OneToOneField

class User(auth_models.AbstractBaseUser, auth_models.PermissionsMixin): 
    matricula = models.OneToOneField(Matricula) 

那麼這個部分很簡單,當你創建視圖時問題就來了,所以如果你根據關係創建你的模型,Django將顯示現有的matricula對象,所以在你的模型中排除這個字段並在視圖中添加模型的母親icula對象,以便您可以創建並保存它。

def add_and_create_matricula(request): 

    if request.method = 'POST': 
     form_user = ModelForm_user(request.POST) 
     form_matricula = ModelForm_matricula(request.POST) 
     if form_user.is_valid and form_matricula.is_valid(): 
      matricula = form_matricula.save() 
      user = form_user.save(commit=False) 
      user.matricula = matricula # Here we add the realtion to the matricula 
      user.save() # And save the relation 

    form_user = ModelForm_user() 
    form_matricula = ModelForm_matricula() 
    return render(request, your_template.html, {'form_user':form_user, 'form_matricula':form_matricula}) 

這裏,我離開你舉個例子,你應該如何matricula對象店內user