2013-02-05 9 views
16

AUTH_USER_MODEL त्रुटि EDIT3 में हल करने में सक्षम नहीं है। पासवर्ड अभी भी फॉर्म के माध्यम से उपयोगकर्ता निर्माण पर सहेज नहीं पाएंगे।AUTH_USER_MODEL मॉडल को संदर्भित करता है .. जिसे स्थापित नहीं किया गया है और बनाया गया है AbstractUser मॉडल

मैं Django 1.5 का उपयोग नए उपयोगकर्ता ओवरराइड/एक्सटेंशन सुविधाओं के साथ खेल रहा हूं, और मैं अपने पंजीकरण फॉर्म के माध्यम से नए उपयोगकर्ताओं को पंजीकृत करने में सक्षम नहीं हूं - केवल व्यवस्थापक के माध्यम से।

Manager isn't available; User has been swapped for 'poker.PokerUser'

models.py:

class PokerUser(AbstractUser): 
    poker_relate = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True) 
    token = models.EmailField() 
    USER_CHOICES = (
     ('1', 'Staker'), 
     ('2', 'Horse') 
    ) 
    user_type = models.CharField(choices=USER_CHOICES, max_length=10) 
    username1 = models.CharField(null=True, blank=True, max_length=40) 
    username2 = models.CharField(null=True, blank=True, max_length=40) 
    username3 = models.CharField(null=True, blank=True, max_length=40) 
    username4 = models.CharField(null=True, blank=True, max_length=40) 
    username5 = models.CharField(null=True, blank=True, max_length=40) 

PokerUserForm मॉडल:

class PokerUserForm(UserCreationForm): 
    class Meta: 
     model = PokerUser 
     fields = ('username','password1','password2','email','user_type','token','username1','username2','username3','username4','username5',) 

मैं का प्रयास किया है बदलने के लिए जब पंजीकरण फार्म के माध्यम से दर्ज की, मैं निम्नलिखित त्रुटि मिलती है मॉडल में का उपयोग करने के लिए मॉडल को स्पष्ट रूप सेसेट करके मॉडल को परिभाषित करने के बजायबजाय model = PokerUser लेकिन फिर मैं निम्न त्रुटि प्राप्त:

django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'poker.PokerUser' that has not been installed 

मेरे AUTH_USER_MODEL में सेटअप है मेरी settings.py तो जैसे:

AUTH_USER_MODEL = 'poker.PokerUser'

पर हम चले - views.py में मेरी पंजीकरण दृश्य:

def UserRegistration(request): 
    player = PokerUser() 

    if request.method == 'POST': 
     form = PokerUserForm(request.POST, instance=player) 
     if form.is_valid(): 
      player.email_address = form.cleaned_data['email'] 
      player.user_type = form.cleaned_data['user_type'] 
      # if player is staker, token is their own email. otherwise their token is their staker's email and 
      # their relation is their staker 
      if player.user_type == '1' or player.user_type == 'staker': 
       player.token = player.email_address 
      else: 
       player.token = form.cleaned_data['token'] 
       staker = PokerUser.objects.get(email=player.token) 
       player.poker_relate = staker 
      player.save() 
      return HttpResponseRedirect('/') 
    else: 
     form = PokerUserForm() 
    initialData = {'form': form} 
    csrfContext = RequestContext(request, initialData) 
    return render_to_response('registration/register.html', csrfContext) 

EDIT1:

According to the docs, UserCreationForm कस्टम उपयोगकर्ता कक्षाओं के उपयोग के लिए पुनर्निर्मित किया जाना चाहिए।

इस प्रकार मैं पूरी UserCreationForm overrode:

class UserCreationForm(forms.ModelForm): 
    """ 
    A form that creates a user, with no privileges, from the given username and 
    password. 
    """ 
    error_messages = { 
     'duplicate_username': _("A user with that username already exists."), 
     'password_mismatch': _("The two password fields didn't match."), 
     } 
    username = forms.RegexField(label=_("Username"), max_length=30, 
     regex=r'^[\[email protected]+-]+$', 
     help_text=_("Required. 30 characters or fewer. Letters, digits and " 
        "@/./+/-/_ only."), 
     error_messages={ 
      'invalid': _("This value may contain only letters, numbers and " 
         "@/./+/-/_ characters.")}) 
    password1 = forms.CharField(label=_("Password"), 
     widget=forms.PasswordInput) 
    password2 = forms.CharField(label=_("Password confirmation"), 
     widget=forms.PasswordInput, 
     help_text=_("Enter the same password as above, for verification.")) 

    class Meta: 
     model = PokerUser 
     fields = ('username','password1','password2','email','user_type','token','username1','username2','username3','username4','username5',) 

    def clean_username(self): 
     # Since User.username is unique, this check is redundant, 
     # but it sets a nicer error message than the ORM. See #13147. 
     username = self.cleaned_data["username"] 
     try: 
      PokerUser.objects.get(username=username) 
     except PokerUser.DoesNotExist: 
      return username 
     raise forms.ValidationError(self.error_messages['duplicate_username']) 

    def clean_password2(self): 
     password1 = self.cleaned_data.get("password1") 
     password2 = self.cleaned_data.get("password2") 
     if password1 and password2 and password1 != password2: 
      raise forms.ValidationError(
       self.error_messages['password_mismatch']) 
     return password2 

    def save(self, commit=True): 
     user = super(UserCreationForm, self).save(commit=False) 
     user.set_password(self.cleaned_data["password1"]) 
     if commit: 
      user.save() 
     return user 

और यह इस त्रुटि को हल करने में सक्षम था:

The Manager isn't available; User has been swapped for 'poker.PokerUser'

अब, उपयोगकर्ताओं को बनाया है, लेकिन में लॉग इन करने में सक्षम नहीं हैं मिलता है जब मैं व्यवस्थापक में उपयोगकर्ताओं की जांच करता हूं, तो पासवर्ड के अलावा सभी जानकारी सही होती है। व्यवस्थापक में मैन्युअल रूप से पासवर्ड जोड़ना सही ढंग से काम नहीं कर रहा है। फिर भी, व्यवस्थापक के माध्यम से उपयोगकर्ताओं को सही ढंग से जोड़ना।

संपादित करें 2:

मैं अभी भी पंजीकरण फार्म के माध्यम से बनाया मेरी AbstractUser से कोई भी मॉडल के रूप में प्रवेश करने में असमर्थ हूँ।मैं पूरी तरह से UserCreationForm अधिरोहित है जैसा कि ऊपर उल्लिखित है, और इस त्रुटि के साथ get_user_model() को लागू करने में असमर्थ हूँ:

AUTH_USER_MODEL refers to model 'poker.PokerUser' that has not been installed

get_user_model() के लिए Django कोड है:

def get_user_model(): 
    "Return the User model that is active in this project" 
    from django.conf import settings 
    from django.db.models import get_model 

    try: 
     app_label, model_name = settings.AUTH_USER_MODEL.split('.') 
    except ValueError: 
     raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form 'app_label.model_name'") 
    user_model = get_model(app_label, model_name) 
    if user_model is None: 
     raise ImproperlyConfigured("AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL) 
    return user_model 

जब से मैं में AUTH_USER_MODEL = 'poker.PokerUser' सेटअप मेरा settings.py, यह काम करना चाहिए। मैंने Django कंसोल के माध्यम से इसे सत्यापित कर लिया है:

>>> from django.contrib.auth import get_user_model 
>>> settings.AUTH_USER_MODEL 
Out[14]: 'poker.PokerUser' 
>>> from django.db.models import get_model 
>>> app_label, model_name = settings.AUTH_USER_MODEL.split('.') 
>>> user_model = get_model(app_label, model_name) 
>>> user_model 
Out[18]: poker.models.PokerUser 

हालांकि कार्यान्वयन अभी भी सही तरीके से काम नहीं करता है।

यदि आपने इसे अभी तक पढ़ा है, धन्यवाद!

EDIT3:

AUTH_USER_MODEL refers to model 'poker.PokerUser' that has not been installed तय किया गया है। मुझे गलती से UserCreationForm था कि मैंने के बजाय poker.models में फिर से बनाया, इसलिए जब मैं get_user_model() चला गया जो poker.PokerUser को सौंपा गया था, तो यह हल नहीं हो सका क्योंकि यह पहले से ही उस स्थान पर था।

अब एकमात्र मुद्दा यह है कि नए उपयोगकर्ताओं को बनाते समय, उनके पासवर्ड सहेजे नहीं जाएंगे। मैं प्रिंट बयान यहाँ रखकर UserCreationForm में एक भी विधि करने के लिए नीचे यह संकुचित है:

def clean_password2(self): 
    password1 = self.cleaned_data.get("password1") 
    print password1 
    password2 = self.cleaned_data.get("password2") 
    print password2 
    if password1 and password2 and password1 != password2: 
     raise forms.ValidationError(
      self.error_messages['password_mismatch']) 
    print password2 
    return password2 

def save(self, commit=True): 
    user = super(UserCreationForm, self).save(commit=False) 
    user.set_password(self.cleaned_data["password1"]) 
    print self.cleaned_data["password1"] 
    if commit: 
     user.save() 
    return user 

clean_password2 प्रदर्शन सादा पाठ पासवर्ड में print password1 और print password1 बयान, लेकिन save विधि में print self.cleaned_data["password1"] खाली है। मेरा फॉर्म डेटा सहेजने के तरीके को क्यों नहीं भेजा जा रहा है?

टीएल; डीआरAbstractUser मॉडल निर्माण दोनों व्यवस्थापक और पंजीकरण फॉर्म के माध्यम से काम कर रहा है, लेकिन केवल व्यवस्थापक के माध्यम से बनाए गए उपयोगकर्ता लॉगिन करने में सक्षम हैं। पंजीकरण फॉर्म के माध्यम से बनाए गए उपयोगकर्ता लॉग इन करने में असमर्थ हैं और पासवर्ड के बिना सहेजे जाने लगते हैं - अन्य सभी जानकारी सही ढंग से सहेजी जाती है।

उत्तर

11

ठीक है मेरे लिए यहां तीन मुद्दे थे, इसलिए मैं उन सभी को संबोधित करने जा रहा हूं क्योंकि मुझे पूरा यकीन है कि पहले दो किसी और के लिए आएंगे।

  • Manager isn't available; User has been swapped for 'poker.PokerUser'

इस का उपयोग करते हुए, लेकिन UserCreationForm पुनः नहीं की वजह से था। 1.5 में कस्टम मॉडल का उपयोग करते समय, कुछ मॉडल फॉर्म बॉक्स के बाहर उपलब्ध हैं लेकिन इसे फिर से बनाया जाना चाहिए। दस्तावेज़ों के लिए here देखें।

  • The Manager isn't available; User has been swapped for 'poker.PokerUser'

मैं अपने settings.py में AUTH_USER_MODEL = 'poker.PokerUser' सेट था, वहीं मैं poker.models स्थान से get_user_model() बुला रहा था। आपको एक अलग स्थान से get_user_model() पर कॉल करना होगा। मेरे फॉर्म को registration.forms पर ले जाना और get_user_model() पर कॉल करना ठीक से काम किया। बचत

  • नए उपयोगकर्ताओं को नहीं

यह मेरा अंत पर सिर्फ एक मस्तिष्क गोज़ था। मेरे UserRegistration मॉडल में मैं फॉर्म से विभिन्न क्षेत्रों में हेरफेर कर रहा था। जब मैंने उन क्षेत्रों को UserCreationForm पर save() विधि के लिए पास कर दिया, तो मैं इसके साथ पासवर्ड फ़ील्ड पास नहीं कर रहा था। ओह!

-5

Django डॉक्स के शब्दों में:

You will also need to register your custom User model with the admin. If your custom User model extends django.contrib.auth.models.AbstractUser, you can use Django's existing django.contrib.auth.admin.UserAdmin class.

चेक अपने admin.py फ़ाइल की सामग्री और यह सुनिश्चित करें कि आप व्यवस्थापक django.contrib.auth.admin.UserAdmin वर्ग का उपयोग कर प्रणाली के साथ अपने कस्टम मॉडल पंजीकृत हैं।

+3

आप डॉक्स विशेष रूप से व्यवस्थापक से संबंधित संदर्भित कर रहे हैं हल। मुद्दा व्यवस्थापक के साथ नहीं है। व्यवस्थापक में उपयोगकर्ताओं को बनाना सही ढंग से काम करता है। मुद्दा व्यवस्थापक के बाहर उपयोगकर्ताओं को बना रहा है। –

+0

सवाल यह है: क्या व्यवस्थापक के साथ बनाए गए सही प्रकार के उपयोगकर्ता हैं? क्या आपने अपने नए मॉडल का उपयोग करने के लिए व्यवस्थापक को कॉन्फ़िगर किया है? – stefanw

+1

जबकि मैं निश्चित रूप से आपके समय और उत्तरों की सराहना करता हूं, मैंने अपनी मूल पोस्ट में इन सभी चिंताओं को दूर करने के लिए काफी प्रयास किए हैं। कृपया अंतिम अनुच्छेद देखें - "अब, उपयोगकर्ता बनाए गए हैं लेकिन लॉग इन करने में सक्षम नहीं हैं। जब मैं व्यवस्थापक में उपयोगकर्ताओं की जांच करता हूं, तो सभी जानकारी पासवर्ड को छोड़कर सही होती है। व्यवस्थापक में मैन्युअल रूप से पासवर्ड जोड़ना सही ढंग से काम नहीं कर रहा है। फिर भी, व्यवस्थापक के माध्यम से उपयोगकर्ताओं को सही तरीके से जोड़ना। " व्यवस्थापक में बनाया गया उपयोगकर्ता सही ढंग से काम करते हैं। 'UserCreationForm' उपयोगकर्ताओं को ओवरराइड करने के बाद अब व्यवस्थापक के बाहर बनाते हैं, लेकिन सही नहीं हैं। –

7

मैंने इसे कुछ बार चलाया है। यह हमेशा एक आयात मुद्दा रहा है। मान लीजिए कि हमारे कोर/models.py कि एक कस्टम उपयोगकर्ता को लागू करता है और एक अन्य फ़ाइल (जैसे कि वरना) से एक प्रतीक का आयात करता है:

from Something import Else 

class CustomUser(AbstractBaseUser): 
    pass 

और फिर हम एक और फ़ाइल CustomUser का उपयोग करता है और यह भी नहीं तो परिभाषित करता है। के यह कुछ/models.py कहते हैं:

from core.models import CustomUser 

class Else(models.Model): 
    pass 

class AnotherClass(models.model): 
    user = models.ForeignKey(CustomUser) 

कोर/models.py वरना आयात करने के लिए हो जाता है, यह कुछ/models.py मूल्यांकन करता है और AnotherClass परिभाषा में चलाता है। AnotherClass कस्टम यूज़र का उपयोग करता है, लेकिन कस्टम यूज़र अभी तक स्थापित नहीं किया गया है क्योंकि हम इसे बनाने की प्रक्रिया में हैं। तो, यह इस त्रुटि को फेंकता है।

मैंने अपने मूल/models.py स्टैंडअलोन को रखकर इस समस्या को हल किया है। यह मेरे अन्य ऐप्स से ज्यादा आयात नहीं करता है।

+0

क्या मैं नाराज हूं क्योंकि मैं पाइथन में नौसिखिया हूं, या पाइथन लोक भी आम तौर पर खराब आयात मुद्दों से परेशान है? प्रोजेक्ट बढ़ने के साथ ही उनका ट्रैक रखना असंभव है! – nakajuice

0

मेरे मामले को अद्यतन करने में, मेटा में उचित app_label इस मुद्दे

class APPUser(AbstractUser): 
    password = models.TextField(blank=True) 

    class Meta: 
    app_label = 'app_auth' 
    db_table = "app_user" 
संबंधित मुद्दे