Opened 2 years ago

Closed 2 years ago

Last modified 23 months ago

#21082 closed Uncategorized (worksforme)

django 1.5.1 custom User models bug

Reported by: xmmilk@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.5
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

(I can't English,sorry!)
on Configurable User models: (I have a third-party reusable application)

the User models primary_key fields name is "uid".
then found the following two error.

1.
AttributeError: 'User' object has no attribute 'id'
django\contrib\admin\templatetags\log.py line 19

user_id = context[self.user].id

modify : user_id = context[self.user].pk

FieldError: Cannot resolve keyword 'id' into field.
django\db\models\sql/query.py line 1332

I do not know how to modify this error.
I just in line 1319 insert :

elif name == 'id':

name = opts.pk.name

Change History (7)

comment:1 Changed 2 years ago by claudep

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to needsinfo
  • Status changed from new to closed

First bug has been solved with #20088.

For the second one, I cannot see exactly what is the problem. Please provide more information (error traceback, example model, test case, ...).

comment:2 Changed 2 years ago by anonymous

Code highlighting:

from django.db import models
from django.utils.http import urlquote
from django.contrib.auth.models import (BaseUserManager,AbstractBaseUser,PermissionsMixin)
from passlib.hash import md5_crypt

class MyUserManager(BaseUserManager):
    def create_user(self,email,user_id,password=None):
        if not user_id:
            raise ValueError('The given username must be set')
        user = self.model(email = MyOaUserManager.normalize_email(email),
            user_id=user_id,is_staff=False,is_active=True,is_superuser=False)
        user.set_password(password)
        user.save(using=self._db)
        return user
    
    def create_superuser(self,email,user_id,password):
        u = self.create_user(email,user_id, password)
        u.is_staff = True
        u.is_active = True
        u.is_superuser = True
        u.save(using=self._db)
        return u

class User(AbstractBaseUser,PermissionsMixin):
    uid = models.AutoField(primary_key=True, db_column='UID') 
    user_id = models.CharField(max_length=20, unique=True, db_column='USER_ID')
    #other fields ...
    is_staff = models.BooleanField(
        verbose_name="staff status",default=False,
        help_text="Designates whether the user can log into this admin site")
    is_active = models.BooleanField(
        verbose_name="active",default=True,
        help_text="Designates whether this user should be treated as active."
        "Unselect this instead of deleting accounts.")
        
    objects = MyUserManager()
    
    USERNAME_FIELD = 'user_id'
    REQUIRED_FIELDS = ['email']
    def get_absolute_url(self):
        return "/users/%s/" %urlquote(self.user_name)
    
    def get_full_name(self):
        return self.user_id
    
    def get_short_name(self):
        return self.user_id
    
    def check_password(self,password,encoded,setter=None,preferred='default'):
        #php crypt. encoded password string as  "$1$SG2.zh..$/9IAOnUoMHEYveEbMNI.v1"
        return md5_crypt.verify(password,encoded)
    
    def make_password(self,password,salt=None,hasher='default'):
        return md5_crypt.encrypt(password)
    
    class Meta:
        app_label = 'base'
        db_table = 'user'

comment:3 Changed 2 years ago by xmmilk@…

Code highlighting:

#sealform views
from django import forms
from sealform import models

class sealform(forms.Form):
    seals = forms.ModelMultipleChoiceField(
            widget=forms.CheckboxSelectMultiple(), queryset=models.seal.objects.all())
    reason = forms.CharField(widget=forms.Textarea)

def index(request):
    form = sealform()
    return render_to_response('sealform.html',{'form':form})

#sealform models
class sealform(models.Model):
    uid = models.IntegerField(max_length=11)
    #some fields...
    seal = models.CommaSeparatedIntegerField(max_length=100)
    
class seal(models.Model):
    name = models.CharField(max_length=50) 
    
    def __unicode__(self):
        return self.name

#templates sealform.html
<form action="sealhandle/" method="post"> 
{% if form.seals %}
    <div class="seals">
        {% for seal in form.seals %}
            {{seal}}
        {% endfor %}
    </div>
{% else %}
    please add seal first!
{% endif %}
<input type="submit" value="submit">
</form>

comment:4 Changed 2 years ago by anonymous

try pass localhost:8000/admin/ to login ,found the flowing errors

Code highlighting:

[10/Sep/2013 20:50:27] "POST /admin/ HTTP/1.1" 302 0
Internal Server Error: /admin/
Traceback (most recent call last):
  File "E:\Python27\lib\site-packages\django\core\handlers\base.py", line 140, i
n get_response
    response = response.render()
  File "E:\Python27\lib\site-packages\django\template\response.py", line 105, in
 render
    self.content = self.rendered_content
  File "E:\Python27\lib\site-packages\django\template\response.py", line 82, in
rendered_content
    content = template.render(context)
  File "E:\Python27\lib\site-packages\django\template\base.py", line 140, in ren
der
    return self._render(context)
  File "E:\Python27\lib\site-packages\django\template\base.py", line 134, in _re
nder
    return self.nodelist.render(context)
  File "E:\Python27\lib\site-packages\django\template\base.py", line 830, in ren
der
    bit = self.render_node(node, context)
  File "E:\Python27\lib\site-packages\django\template\debug.py", line 74, in ren
der_node
    return node.render(context)
  File "E:\Python27\lib\site-packages\django\template\loader_tags.py", line 124,
 in render
    return compiled_parent._render(context)
  File "E:\Python27\lib\site-packages\django\template\base.py", line 134, in _re
nder
    return self.nodelist.render(context)
  File "E:\Python27\lib\site-packages\django\template\base.py", line 830, in ren
der
    bit = self.render_node(node, context)
  File "E:\Python27\lib\site-packages\django\template\debug.py", line 74, in ren
der_node
    return node.render(context)
  File "E:\Python27\lib\site-packages\django\template\loader_tags.py", line 124,
 in render
    return compiled_parent._render(context)
  File "E:\Python27\lib\site-packages\django\template\base.py", line 134, in _re
nder
    return self.nodelist.render(context)
  File "E:\Python27\lib\site-packages\django\template\base.py", line 830, in ren
der
    bit = self.render_node(node, context)
  File "E:\Python27\lib\site-packages\django\template\debug.py", line 74, in ren
der_node
    return node.render(context)
  File "E:\Python27\lib\site-packages\django\template\loader_tags.py", line 63,
in render
    result = block.nodelist.render(context)
  File "E:\Python27\lib\site-packages\django\template\base.py", line 830, in ren
der
    bit = self.render_node(node, context)
  File "E:\Python27\lib\site-packages\django\template\debug.py", line 74, in ren
der_node
    return node.render(context)
  File "E:\Python27\lib\site-packages\django\contrib\admin\templatetags\log.py",
 line 20, in render
    context[self.varname] = LogEntry.objects.filter(user__id__exact=user_id).sel
ect_related('content_type', 'user')[:int(self.limit)]
  File "E:\Python27\lib\site-packages\django\db\models\manager.py", line 155, in
 filter
    return self.get_query_set().filter(*args, **kwargs)
  File "E:\Python27\lib\site-packages\django\db\models\query.py", line 655, in f
ilter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "E:\Python27\lib\site-packages\django\db\models\query.py", line 673, in _
filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "E:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1266,
 in add_q
    can_reuse=used_aliases, force_having=force_having)
  File "E:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1134,
 in add_filter
    process_extras=process_extras)
  File "E:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1332,
 in setup_joins
    "Choices are: %s" % (name, ", ".join(names)))
FieldError: Cannot resolve keyword 'id' into field. Choices are: add_home, autho
rize, avatar, bbs_counter, bbs_signature, bind_ip, birthday, bkground, bp_no, by
name, call_sound, canbroadcast, concern_user, dept_id, dept_id_other, disabled,
duty_type, email, email_capacity, fax_no_dept, folder_capacity, groups, icq_no,
im_range, is_active, is_lunar, is_staff, is_superuser, key_sn, last_login, last_
pass_time, last_visit_ip, last_visit_time, limit_login, logentry, menu_expand, m
enu_image, menu_type, mobil_no, mobil_no_hidden, mobile_ps1, mobile_ps2, mobile_
sp, msn, my_rss, my_status, mytable_left, mytable_right, nick_name, not_login, n
ot_search, not_view_table, not_view_user, oicq_no, on_status, online, panel, pas
sword, photo, pic_id, portal, post_dept, post_no_home, post_priv, remark, score,
 secure_key_sn, sex, shortcut, show_rss, sms_on, tder_flag, tel_no_dept, tel_no_
home, theme, uid, uin, useing_key, user_define, user_id, user_name, user_no, use
r_permissions, user_priv, user_priv_other, using_finger, weather_city, webmail_c
apacity, webmail_num
[10/Sep/2013 20:50:28] "GET /admin/ HTTP/1.1" 500 256442

comment:5 Changed 2 years ago by claudep

Thanks for the details. Could you try the same code with Django 1.6?

comment:6 Changed 23 months ago by xmmilk@…

Yes. It works very well with Django 1.6
Thanks so much for your help, and i'm so sorry to wasted your time.

comment:7 Changed 23 months ago by claudep

  • Resolution changed from needsinfo to worksforme

You are welcome. And yes, testing bugs with the latest version of Django is a good practice :-)

Note: See TracTickets for help on using tickets.
Back to Top