Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Django model question from a newbee [RSS Feed]

#1 Dec. 16, 2010 12:39:30

R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django model question from a newbee


I am new to Django, and I am building a simple application where I am
going to manage a legacy user database from Djangos admin interface.
Below is how my model looks like.

My goal is to hide some of the fields in the form where I edit/add
users. However, currently I can see all the fields except from the Id
field. Furhermore, the password field is stored in clear text, but is
supposed to be stored as an md5 hash, and the password is visible in
clear text in the form. Any hints what I am doing wrong, and how to
improve things?

from django.db import models
from django.contrib import admin
from django.forms import ModelForm, PasswordInput

import datetime
try:
from hashlib import md5
except:
from md5 import new as md5

class CasDomain(models.Model):
name = models.CharField(max_length=64, primary_key=True)
class Meta:
db_table = u'cas_domain'

def __unicode__(self):
return self.name

class CasUser(models.Model):
ACCESS_LEVEL = (
(0, 'Administrator'),
(1, 'Manager'),
(2, 'Domain Manager'),
(3, 'Email'),
(255, 'None'),
)

id = models.AutoField(primary_key=True)
username = models.CharField(max_length=64)
password = models.CharField(max_length=32)
email = models.EmailField()
first_name = models.CharField(max_length=64, blank=True)
last_name = models.CharField(max_length=64, blank=True)
froxlor_mail_id = models.IntegerField(null=True, blank=True)
access_level = models.IntegerField(choices=ACCESS_LEVEL)
domain = models.ForeignKey(CasDomain,db_column='domain')
mod_date = models.DateField()
pub_date = models.DateField()
class Meta:
db_table = u'cas_user'

def __unicode__(self):
return self.username

def __unicode__(self):
return self.password

def __unicode__(self):
return self.first_name

def __unicode__(self):
return self.last_name

class CasUserForm(ModelForm):
class Meta:
model = CasUser
fields = ('username', 'first_name', 'first_name', 'email',
'access_level')
widgets = {
'password': PasswordInput(),
}

def save(self):
if not self.id:
self.pub_date = datetime.date.today()
self.password = md5(self.password).hexdigest()
self.mod_date = datetime.datetime.today()
super(CasUserForm, self).save()

class CasUserAdmin(admin.ModelAdmin):
class Meta:
form = CasUserForm

# Make sure the classes are available in the admin interface
admin.site.register(CasDomain)
admin.site.register(CasUser, CasUserAdmin)

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 16, 2010 12:47:24

Tom E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django model question from a newbee


On Thu, Dec 16, 2010 at 12:39 PM, Reino <re...@oribium.se> wrote:
> I am new to Django, and I am building a simple application where I am
> going to manage a legacy user database from Djangos admin interface.
> Below is how my model looks like.
>
> My goal is to hide some of the fields in the form where I edit/add
> users. However, currently I can see all the fields except from the Id
> field. Furhermore, the password field is stored in clear text, but is
> supposed to be stored as an md5 hash, and the password is visible in
> clear text in the form. Any hints what I am doing wrong, and how to
> improve things?
>
> from django.db import models
> from django.contrib import admin
> from django.forms import ModelForm, PasswordInput
>
> import datetime
> try:
>        from hashlib import md5
> except:
>        from md5 import new as md5
>
> class CasDomain(models.Model):
>        name = models.CharField(max_length=64, primary_key=True)
>        class Meta:
>                db_table = u'cas_domain'
>
>        def __unicode__(self):
>                return self.name
>
> class CasUser(models.Model):
>        ACCESS_LEVEL = (
>        (0, 'Administrator'),
>        (1, 'Manager'),
>        (2, 'Domain Manager'),
>        (3, 'Email'),
>        (255, 'None'),
>        )
>
>        id = models.AutoField(primary_key=True)
>        username = models.CharField(max_length=64)
>        password = models.CharField(max_length=32)
>        email = models.EmailField()
>        first_name = models.CharField(max_length=64, blank=True)
>        last_name = models.CharField(max_length=64, blank=True)
>        froxlor_mail_id = models.IntegerField(null=True, blank=True)
>        access_level = models.IntegerField(choices=ACCESS_LEVEL)
>        domain = models.ForeignKey(CasDomain,db_column='domain')
>        mod_date = models.DateField()
>        pub_date = models.DateField()
>        class Meta:
>                db_table = u'cas_user'
>
>        def __unicode__(self):
>                return self.username
>
>        def __unicode__(self):
>                return self.password
>
>        def __unicode__(self):
>                return self.first_name
>
>        def __unicode__(self):
>                return self.last_name
>
> class CasUserForm(ModelForm):
>        class Meta:
>                model = CasUser
>                fields = ('username', 'first_name', 'first_name', 'email',
> 'access_level')
>                widgets = {
>                        'password': PasswordInput(),
>                }
>
>        def save(self):
>                if not self.id:
>                        self.pub_date = datetime.date.today()
>                        self.password = md5(self.password).hexdigest()
>                self.mod_date = datetime.datetime.today()
>                super(CasUserForm, self).save()
>
> class CasUserAdmin(admin.ModelAdmin):
>        class Meta:
>                form = CasUserForm
>
> # Make sure the classes are available in the admin interface
> admin.site.register(CasDomain)
> admin.site.register(CasUser, CasUserAdmin)
>

I'd recommend you use contrib.auth, rather than roll your own.

I would have thought the first issue is that you are confusing an
instance of a ModelForm with an instance of the Model it represents.
Your save() method should look more like this:


def save(self, commit=False):
instance = super(CasUserForm, self).save(commit=False)
if not instance.id:
instance.pub_date = datetime.date.today()
instance.password = md5(instance.password).hexdigest()
instance.mod_date = datetime.datetime.today()
if commit:
instance.save()
return instance

Finally, if you want a modified field, it should simply be defined as
models.DateTimeField(auto_now=True), no need to handle it manually.

HTH

Tom

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 16, 2010 17:53:08

M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django model question from a newbee


OK So these are UI issues and how you are saving Users to the
database. The database field itself is a CharField.

1.) To hide form elements add a tupple inside the FormField under
class meta like so:
class CasUserForm(ModelForm):

exclude =

2.)

On Dec 16, 6:39 am, Reino <re...@oribium.se> wrote:
> I am new to Django, and I am building a simple application where I am
> going to manage a legacy user database from Djangos admin interface.
> Below is how my model looks like.
>
> My goal is to hide some of the fields in the form where I edit/add
> users. However, currently I can see all the fields except from the Id
> field. Furhermore, the password field is stored in clear text, but is
> supposed to be stored as an md5 hash, and the password is visible in
> clear text in the form. Any hints what I am doing wrong, and how to
> improve things?
>
> from django.db import models
> from django.contrib import admin
> from django.forms import ModelForm, PasswordInput
>
> import datetime
> try:
>         from hashlib import md5
> except:
>         from md5 import new as md5
>
> class CasDomain(models.Model):
>         name = models.CharField(max_length=64, primary_key=True)
>         class Meta:
>                 db_table = u'cas_domain'
>
>         def __unicode__(self):
>                 return self.name
>
> class CasUser(models.Model):
>         ACCESS_LEVEL = (
>         (0, 'Administrator'),
>         (1, 'Manager'),
>         (2, 'Domain Manager'),
>         (3, 'Email'),
>         (255, 'None'),
>         )
>
>         id = models.AutoField(primary_key=True)
>         username = models.CharField(max_length=64)
>         password = models.CharField(max_length=32)
>         email = models.EmailField()
>         first_name = models.CharField(max_length=64, blank=True)
>         last_name = models.CharField(max_length=64, blank=True)
>         froxlor_mail_id = models.IntegerField(null=True, blank=True)
>         access_level = models.IntegerField(choices=ACCESS_LEVEL)
>         domain = models.ForeignKey(CasDomain,db_column='domain')
>         mod_date = models.DateField()
>         pub_date = models.DateField()
>         class Meta:
>                 db_table = u'cas_user'
>
>         def __unicode__(self):
>                 return self.username
>
>         def __unicode__(self):
>                 return self.password
>
>         def __unicode__(self):
>                 return self.first_name
>
>         def __unicode__(self):
>                 return self.last_name
>
> class CasUserForm(ModelForm):
>         class Meta:
>                 model = CasUser
>                 fields = ('username', 'first_name', 'first_name', 'email',
> 'access_level')
>                 widgets = {
>                         'password': PasswordInput(),
>                 }
>
>         def save(self):
>                 if not self.id:
>                         self.pub_date = datetime.date.today()
>                         self.password = md5(self.password).hexdigest()
>                 self.mod_date = datetime.datetime.today()
>                 super(CasUserForm, self).save()
>
> class CasUserAdmin(admin.ModelAdmin):
>         class Meta:
>                 form = CasUserForm
>
> # Make sure the classes are available in the admin interface
> admin.site.register(CasDomain)
> admin.site.register(CasUser, CasUserAdmin)

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Dec. 16, 2010 18:07:13

M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django model question from a newbee


Ok I did not mean to send that last post, it was an accident.

1.) Add exclude tuple here:
class CasUserForm(ModelForm):
class Meta:
exclude = ('field1', 'field2')

Now go ahead with part 2)

2.) Hide passwords in the form either by manually calling the fields
and specifying a field with the correct name like:
<input type="password" size="25">
or in the form you can do this:
attrs_dict = { 'class': 'required' }
class RegistrationForm(forms.Form):
password1 =
forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict,
render_value=False),
label=_("Password:"))

3.) Encrypting the password before storing it to the database is easy
by calling the right Django method:
User.objects.create_user(username, email, password)


Hope this helps,
Matteius



On Dec 16, 6:47 am, Tom Evans <tevans...@googlemail.com> wrote:
> On Thu, Dec 16, 2010 at 12:39 PM, Reino <re...@oribium.se> wrote:
> > I am new to Django, and I am building a simple application where I am
> > going to manage a legacy user database from Djangos admin interface.
> > Below is how my model looks like.
>
> > My goal is to hide some of the fields in the form where I edit/add
> > users. However, currently I can see all the fields except from the Id
> > field. Furhermore, the password field is stored in clear text, but is
> > supposed to be stored as an md5 hash, and the password is visible in
> > clear text in the form. Any hints what I am doing wrong, and how to
> > improve things?
>
> > from django.db import models
> > from django.contrib import admin
> > from django.forms import ModelForm, PasswordInput
>
> > import datetime
> > try:
> >        from hashlib import md5
> > except:
> >        from md5 import new as md5
>
> > class CasDomain(models.Model):
> >        name = models.CharField(max_length=64, primary_key=True)
> >        class Meta:
> >                db_table = u'cas_domain'
>
> >        def __unicode__(self):
> >                return self.name
>
> > class CasUser(models.Model):
> >        ACCESS_LEVEL = (
> >        (0, 'Administrator'),
> >        (1, 'Manager'),
> >        (2, 'Domain Manager'),
> >        (3, 'Email'),
> >        (255, 'None'),
> >        )
>
> >        id = models.AutoField(primary_key=True)
> >        username = models.CharField(max_length=64)
> >        password = models.CharField(max_length=32)
> >        email = models.EmailField()
> >        first_name = models.CharField(max_length=64, blank=True)
> >        last_name = models.CharField(max_length=64, blank=True)
> >        froxlor_mail_id = models.IntegerField(null=True, blank=True)
> >        access_level = models.IntegerField(choices=ACCESS_LEVEL)
> >        domain = models.ForeignKey(CasDomain,db_column='domain')
> >        mod_date = models.DateField()
> >        pub_date = models.DateField()
> >        class Meta:
> >                db_table = u'cas_user'
>
> >        def __unicode__(self):
> >                return self.username
>
> >        def __unicode__(self):
> >                return self.password
>
> >        def __unicode__(self):
> >                return self.first_name
>
> >        def __unicode__(self):
> >                return self.last_name
>
> > class CasUserForm(ModelForm):
> >        class Meta:
> >                model = CasUser
> >                fields = ('username', 'first_name', 'first_name', 'email',
> > 'access_level')
> >                widgets = {
> >                        'password': PasswordInput(),
> >                }
>
> >        def save(self):
> >                if not self.id:
> >                        self.pub_date = datetime.date.today()
> >                        self.password = md5(self.password).hexdigest()
> >                self.mod_date = datetime.datetime.today()
> >                super(CasUserForm, self).save()
>
> > class CasUserAdmin(admin.ModelAdmin):
> >        class Meta:
> >                form = CasUserForm
>
> > # Make sure the classes are available in the admin interface
> > admin.site.register(CasDomain)
> > admin.site.register(CasUser, CasUserAdmin)
>
> I'd recommend you use contrib.auth, rather than roll your own.
>
> I would have thought the first issue is that you are confusing an
> instance of a ModelForm with an instance of the Model it represents.
> Your save() method should look more like this:
>
> def save(self, commit=False):
>     instance = super(CasUserForm, self).save(commit=False)
>     if not instance.id:
>         instance.pub_date = datetime.date.today()
>         instance.password = md5(instance.password).hexdigest()
>     instance.mod_date = datetime.datetime.today()
>     if commit:
>         instance.save()
>     return instance
>
> Finally, if you want a modified field, it should simply be defined as
> models.DateTimeField(auto_now=True), no need to handle it manually.
>
> HTH
>
> Tom

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Dec. 16, 2010 20:05:49

R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django model question from a newbee


Thanks Tom,

I was confusing an instance of a ModelForm with an instance of the
Model it represents. You suggestion about the mod_time is good idea.
However, I still can't get the password stored as an md5 hash.

Matteius, it is not an option to use Djangos User object, and user
database.

Matteius. since the fields that are supposed to be excuded with the
exclude statemant, are still visible, I figure I am doing things
wrong.

The password is still stored in clear text, and shown in clear text in
the form.

I modified the model slightly, and put a few comment is in it.

My new model:

from django.db import models
from django.contrib import admin
from django.forms import ModelForm, PasswordInput, CharField

import datetime
try:
from hashlib import md5
except:
from md5 import new as md5

class CasDomain(models.Model):
name = models.CharField(max_length=64, primary_key=True)
class Meta:
db_table = u'cas_domain'

def __unicode__(self):
return self.name

class CasUser(models.Model):
ACCESS_LEVEL = (
(0, 'Administrator'),
(1, 'Manager'),
(2, 'Domain Manager'),
(3, 'Email'),
(255, 'None'),
)

id = models.AutoField(primary_key=True)
username = models.CharField(max_length=64)
password = models.CharField(max_length=32)
email = models.EmailField()
first_name = models.CharField(max_length=64, blank=True)
last_name = models.CharField(max_length=64, blank=True)
froxlor_mail_id = models.IntegerField(null=True, blank=True)
access_level = models.IntegerField(choices=ACCESS_LEVEL)
domain = models.ForeignKey(CasDomain,db_column='domain')
mod_time = models.DateTimeField(auto_now=True)
pub_time = models.DateTimeField()
class Meta:
db_table = u'cas_user'

def __unicode__(self):
return self.username

def __unicode__(self):
return self.password

def __unicode__(self):
return self.first_name

def __unicode__(self):
return self.last_name

class CasUserForm(ModelForm):
# password = CharField(widget=PasswordInput())
class Meta:
model = CasUser
# Neiter the exclude line or the fields line below make any field to
be excluded
# in the admin form
exclude = ('froxlor_mail_id', 'mod_time','pub_time')
# fields = ('username', 'password', 'first_name', 'last_name',
'email', 'access_level')
widgets = {
'password': PasswordInput()
}

def save(self, commit=False):
instance = super(CasUserForm, self).save(commit=False)
if not instance.id:
instance.pub_time = datetime.datetime.now
instance.password = md5(instance.password).hexdigest()
if commit:
instance.save()
return instance

class CasUserAdmin(admin.ModelAdmin):
# If i uncomment the line below, only the desired fields are present
in the form
# However then the pub_time is not stored and cause a error, since
pub_time is mandatory
# fields = ('username', 'password', 'first_name', 'last_name',
'email', 'access_level')
class Meta:
form = CasUserForm

# Make sure the classes are available in the admin interface
admin.site.register(CasDomain)
admin.site.register(CasUser, CasUserAdmin)


On 16 Dec, 19:07, Matteius <matte...@gmail.com> wrote:
> Ok I did not mean to send that last post, it was an accident.
>
> 1.) Add exclude tuple here:
> class CasUserForm(ModelForm):
>         class Meta:
>               exclude = ('field1', 'field2')
>
> Now go ahead with part 2)
>
> 2.) Hide passwords in the form either by manually calling the fields
> and specifying a field with the correct name like:
> <input type="password" size="25">
> or in the form you can do this:
> attrs_dict = { 'class': 'required' }
> class RegistrationForm(forms.Form):
>     password1 =
> forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict,
> render_value=False),
>                                 label=_("Password:"))
>
> 3.) Encrypting the password before storing it to the database is easy
> by calling the right Django method:
> User.objects.create_user(username, email, password)
>
> Hope this helps,
> Matteius
>
> On Dec 16, 6:47 am, Tom Evans <tevans...@googlemail.com> wrote:
>
> > On Thu, Dec 16, 2010 at 12:39 PM, Reino <re...@oribium.se> wrote:
> > > I am new to Django, and I am building a simple application where I am
> > > going to manage a legacy user database from Djangos admin interface.
> > > Below is how my model looks like.
>
> > > My goal is to hide some of the fields in the form where I edit/add
> > > users. However, currently I can see all the fields except from the Id
> > > field. Furhermore, the password field is stored in clear text, but is
> > > supposed to be stored as an md5 hash, and the password is visible in
> > > clear text in the form. Any hints what I am doing wrong, and how to
> > > improve things?
>
> > > from django.db import models
> > > from django.contrib import admin
> > > from django.forms import ModelForm, PasswordInput
>
> > > import datetime
> > > try:
> > >        from hashlib import md5
> > > except:
> > >        from md5 import new as md5
>
> > > class CasDomain(models.Model):
> > >        name = models.CharField(max_length=64, primary_key=True)
> > >        class Meta:
> > >                db_table = u'cas_domain'
>
> > >        def __unicode__(self):
> > >                return self.name
>
> > > class CasUser(models.Model):
> > >        ACCESS_LEVEL = (
> > >        (0, 'Administrator'),
> > >        (1, 'Manager'),
> > >        (2, 'Domain Manager'),
> > >        (3, 'Email'),
> > >        (255, 'None'),
> > >        )
>
> > >        id = models.AutoField(primary_key=True)
> > >        username = models.CharField(max_length=64)
> > >        password = models.CharField(max_length=32)
> > >        email = models.EmailField()
> > >        first_name = models.CharField(max_length=64, blank=True)
> > >        last_name = models.CharField(max_length=64, blank=True)
> > >        froxlor_mail_id = models.IntegerField(null=True, blank=True)
> > >        access_level = models.IntegerField(choices=ACCESS_LEVEL)
> > >        domain = models.ForeignKey(CasDomain,db_column='domain')
> > >        mod_date = models.DateField()
> > >        pub_date = models.DateField()
> > >        class Meta:
> > >                db_table = u'cas_user'
>
> > >        def __unicode__(self):
> > >                return self.username
>
> > >        def __unicode__(self):
> > >                return self.password
>
> > >        def __unicode__(self):
> > >                return self.first_name
>
> > >        def __unicode__(self):
> > >                return self.last_name
>
> > > class CasUserForm(ModelForm):
> > >        class Meta:
> > >                model = CasUser
> > >                fields = ('username', 'first_name', 'first_name', 'email',
> > > 'access_level')
> > >                widgets = {
> > >                        'password': PasswordInput(),
> > >                }
>
> > >        def save(self):
> > >                if not self.id:
> > >                        self.pub_date = datetime.date.today()
> > >                        self.password = md5(self.password).hexdigest()
> > >                self.mod_date = datetime.datetime.today()
> > >                super(CasUserForm, self).save()
>
> > > class CasUserAdmin(admin.ModelAdmin):
> > >        class Meta:
> > >                form = CasUserForm
>
> > > # Make sure the classes are available in the admin interface
> > > admin.site.register(CasDomain)
> > > admin.site.register(CasUser, CasUserAdmin)
>
> > I'd recommend you use contrib.auth, rather than roll your own.
>
> > I would have thought the first issue is that you are confusing an
> > instance of a ModelForm with an instance of the Model it represents.
> > Your save() method should look more like this:
>
> > def save(self, commit=False):
> >     instance = super(CasUserForm, self).save(commit=False)
> >     if not instance.id:
> >         instance.pub_date = datetime.date.today()
> >         instance.password = md5(instance.password).hexdigest()
> >     instance.mod_date = datetime.datetime.today()
> >     if commit:
> >         instance.save()
> >     return instance
>
> > Finally, if you want a modified field, it should simply be defined as
> > models.DateTimeField(auto_now=True), no need to handle it manually.
>
> > HTH
>
> > Tom

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 16th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support