Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 June 17, 2010 06:14:59

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

DateField issues


I've defined a number of models, one of which I've called
MemberProfile that looks like this

class MemberProfile(models.Model):
postal_addr1=models.CharField(max_length=50, verbose_name="postal
address line 1")
postal_addr2=models.CharField(max_length=50, verbose_name="postal
address line 2")
postal_addr3=models.CharField(max_length=50, verbose_name="postal
address line 3")
postalcode=models.CharField(max_length=4, verbose_name="postal
code")
res_addr1=models.CharField(max_length=50,
verbose_name="residential address line 1")
res_addr2=models.CharField(max_length=50,
verbose_name="residential address line 2")
res_addr3=models.CharField(max_length=50,
verbose_name="residential address line 3")
rescode=models.CharField(max_length=4, verbose_name="residential
postal code")
homeno=models.CharField(max_length=12, verbose_name="home number")
workno=models.CharField(max_length=12, verbose_name="work number")
cellno=models.CharField(max_length=12, unique=True,
verbose_name="cellphone number")
idno=models.CharField(max_length=13, unique=True, verbose_name="id
or passport number")
occu=models.CharField(max_length=15, verbose_name="occupation")
employer=models.CharField(max_length=30)
amount_paid=models.IntegerField(blank=True, null=True,
help_text="total fees paid to date", verbose_name="total reciepts")
reciept_no=models.IntegerField(blank=True, null=True,
help_text="latest reciept number", verbose_name="reciept number")
dob=models.DateField(verbose_name="date of birth")
start_date=models.DateField()
expire_date=models.DateField()
captured_by=models.CharField(max_length=50, help_text="name of
data capturer")
photo=models.ImageField(upload_to="memberphotos", null=True,
blank=True)
permission=models.CharField(max_length=1,
choices=PERMISSION_CHOICES, help_text="Administration level, if the
user is a superuser this field has no effect")
user=models.ForeignKey(User,unique=True,related_name="member",
editable=False)
branch=models.ForeignKey(Branch,related_name="member", null=True)

def create_member(self,username,email,password):
m=MemberProfile()

m.user=User.objects.create_user(username=username,email=email,password=password)
return m

def __unicode__(self):
return self.user.username

def save(self,*args,**kwargs):
if not self.start_date:
self.start_date=models.DateField(datetime.date.today())
year=timedelta(days=365)
self.expire_date=models.DateField(datetime.date.today()
+year)
super(MemberProfile, self).save(self,*args,**kwargs)


I then wrote a method called populate just to test the database out
and it is pretty friendly until I get this:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/host/Shared info/MyDBA/MyMembers/populate.py", line 114, in
populate
m1.save()
File "/host/Shared info/MyDBA/MyMembers/../MyMembers/chiefs/
models.py", line 115, in save
super(MemberProfile, self).save(self,*args,**kwargs)
File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line
410, in save
self.save_base(force_insert=force_insert,
force_update=force_update)
File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line
483, in save_base
values =
File "/usr/lib/pymodules/python2.6/django/db/models/fields/
__init__.py", line 192, in get_db_prep_save
return self.get_db_prep_value(value)
File "/usr/lib/pymodules/python2.6/django/db/models/fields/
__init__.py", line 511, in get_db_prep_value
return connection.ops.value_to_db_date(self.to_python(value))
File "/usr/lib/pymodules/python2.6/django/db/models/fields/
__init__.py", line 472, in to_python
if not ansi_date_re.search(value):
TypeError: expected string or buffer

m1 is an instance of MemberProfile...

The error goes away if I comment out everything to do with
DateField...
Am I doing something retarded? As far as I can see I'm populating the
fields the right way...

Any help would be greatly appreciated

--
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 June 17, 2010 06:24:55

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

DateField issues


read about autofill_now=True and autofill=True

On Thu, Jun 17, 2010 at 8:14 AM, Sheena <sheena.oconn...@gmail.com> wrote:

> I've defined a number of models, one of which I've called
> MemberProfile that looks like this
>
> class MemberProfile(models.Model):
> postal_addr1=models.CharField(max_length=50, verbose_name="postal
> address line 1")
> postal_addr2=models.CharField(max_length=50, verbose_name="postal
> address line 2")
> postal_addr3=models.CharField(max_length=50, verbose_name="postal
> address line 3")
> postalcode=models.CharField(max_length=4, verbose_name="postal
> code")
> res_addr1=models.CharField(max_length=50,
> verbose_name="residential address line 1")
> res_addr2=models.CharField(max_length=50,
> verbose_name="residential address line 2")
> res_addr3=models.CharField(max_length=50,
> verbose_name="residential address line 3")
> rescode=models.CharField(max_length=4, verbose_name="residential
> postal code")
> homeno=models.CharField(max_length=12, verbose_name="home number")
> workno=models.CharField(max_length=12, verbose_name="work number")
> cellno=models.CharField(max_length=12, unique=True,
> verbose_name="cellphone number")
> idno=models.CharField(max_length=13, unique=True, verbose_name="id
> or passport number")
> occu=models.CharField(max_length=15, verbose_name="occupation")
> employer=models.CharField(max_length=30)
> amount_paid=models.IntegerField(blank=True, null=True,
> help_text="total fees paid to date", verbose_name="total reciepts")
> reciept_no=models.IntegerField(blank=True, null=True,
> help_text="latest reciept number", verbose_name="reciept number")
> dob=models.DateField(verbose_name="date of birth")
> start_date=models.DateField()
> expire_date=models.DateField()
> captured_by=models.CharField(max_length=50, help_text="name of
> data capturer")
> photo=models.ImageField(upload_to="memberphotos", null=True,
> blank=True)
> permission=models.CharField(max_length=1,
> choices=PERMISSION_CHOICES, help_text="Administration level, if the
> user is a superuser this field has no effect")
> user=models.ForeignKey(User,unique=True,related_name="member",
> editable=False)
> branch=models.ForeignKey(Branch,related_name="member", null=True)
>
> def create_member(self,username,email,password):
> m=MemberProfile()
>
>
> m.user=User.objects.create_user(username=username,email=email,password=password)
> return m
>
> def __unicode__(self):
> return self.user.username
>
> def save(self,*args,**kwargs):
> if not self.start_date:
> self.start_date=models.DateField(datetime.date.today())
> year=timedelta(days=365)
> self.expire_date=models.DateField(datetime.date.today()
> +year)
> super(MemberProfile, self).save(self,*args,**kwargs)
>
>
> I then wrote a method called populate just to test the database out
> and it is pretty friendly until I get this:
>
> Traceback (most recent call last):
> File "<console>", line 1, in <module>
> File "/host/Shared info/MyDBA/MyMembers/populate.py", line 114, in
> populate
> m1.save()
> File "/host/Shared info/MyDBA/MyMembers/../MyMembers/chiefs/
> models.py", line 115, in save
> super(MemberProfile, self).save(self,*args,**kwargs)
> File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line
> 410, in save
> self.save_base(force_insert=force_insert,
> force_update=force_update)
> File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line
> 483, in save_base
> values =
> File "/usr/lib/pymodules/python2.6/django/db/models/fields/
> __init__.py", line 192, in get_db_prep_save
> return self.get_db_prep_value(value)
> File "/usr/lib/pymodules/python2.6/django/db/models/fields/
> __init__.py", line 511, in get_db_prep_value
> return connection.ops.value_to_db_date(self.to_python(value))
> File "/usr/lib/pymodules/python2.6/django/db/models/fields/
> __init__.py", line 472, in to_python
> if not ansi_date_re.search(value):
> TypeError: expected string or buffer
>
> m1 is an instance of MemberProfile...
>
> The error goes away if I comment out everything to do with
> DateField...
> Am I doing something retarded? As far as I can see I'm populating the
> fields the right way...
>
> Any help would be greatly appreciated
>
> --
> 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<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>
>

--
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 June 17, 2010 06:40:08

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

DateField issues


Thanks

The question is how do I populate a field with some other date, for
example, there's a date of birth field that the auto stuff wont be
ideal for...
In the populate method i mentioned before, i passed the dob(date of
birth) field a Date object initialized to something arbitrary. Does
the DateField not get along with standard date objects? What format
should stuff be in for populating DateFields?

On Jun 17, 7:24 am, Alexander Jeliuc <jeliucalexa...@gmail.com> wrote:
> read about autofill_now=True and autofill=True
>
> On Thu, Jun 17, 2010 at 8:14 AM, Sheena <sheena.oconn...@gmail.com> wrote:
> > I've defined a number of models, one of which I've called
> > MemberProfile that looks like this
>
> > class MemberProfile(models.Model):
> >    postal_addr1=models.CharField(max_length=50, verbose_name="postal
> > address line 1")
> >    postal_addr2=models.CharField(max_length=50, verbose_name="postal
> > address line 2")
> >    postal_addr3=models.CharField(max_length=50, verbose_name="postal
> > address line 3")
> >    postalcode=models.CharField(max_length=4, verbose_name="postal
> > code")
> >    res_addr1=models.CharField(max_length=50,
> > verbose_name="residential address line 1")
> >    res_addr2=models.CharField(max_length=50,
> > verbose_name="residential address line 2")
> >    res_addr3=models.CharField(max_length=50,
> > verbose_name="residential address line 3")
> >    rescode=models.CharField(max_length=4, verbose_name="residential
> > postal code")
> >    homeno=models.CharField(max_length=12, verbose_name="home number")
> >    workno=models.CharField(max_length=12, verbose_name="work number")
> >    cellno=models.CharField(max_length=12, unique=True,
> > verbose_name="cellphone number")
> >    idno=models.CharField(max_length=13, unique=True, verbose_name="id
> > or passport number")
> >    occu=models.CharField(max_length=15, verbose_name="occupation")
> >    employer=models.CharField(max_length=30)
> >    amount_paid=models.IntegerField(blank=True, null=True,
> > help_text="total fees paid to date", verbose_name="total reciepts")
> >    reciept_no=models.IntegerField(blank=True, null=True,
> > help_text="latest reciept number", verbose_name="reciept number")
> >    dob=models.DateField(verbose_name="date of birth")
> >    start_date=models.DateField()
> >    expire_date=models.DateField()
> >    captured_by=models.CharField(max_length=50, help_text="name of
> > data capturer")
> >    photo=models.ImageField(upload_to="memberphotos", null=True,
> > blank=True)
> >    permission=models.CharField(max_length=1,
> > choices=PERMISSION_CHOICES, help_text="Administration level, if the
> > user is a superuser this field has no effect")
> >    user=models.ForeignKey(User,unique=True,related_name="member",
> > editable=False)
> >    branch=models.ForeignKey(Branch,related_name="member", null=True)
>
> >    def create_member(self,username,email,password):
> >        m=MemberProfile()
>
> > m.user=User.objects.create_user(username=username,email=email,password=password)
> >        return m
>
> >    def __unicode__(self):
> >        return self.user.username
>
> >    def save(self,*args,**kwargs):
> >        if not self.start_date:
> >            self.start_date=models.DateField(datetime.date.today())
> >            year=timedelta(days=365)
> >            self.expire_date=models.DateField(datetime.date.today()
> > +year)
> >        super(MemberProfile, self).save(self,*args,**kwargs)
>
> > I then wrote a method called populate just to test the database out
> > and it is pretty friendly until I get this:
>
> > Traceback (most recent call last):
> >  File "<console>", line 1, in <module>
> >  File "/host/Shared info/MyDBA/MyMembers/populate.py", line 114, in
> > populate
> >    m1.save()
> >  File "/host/Shared info/MyDBA/MyMembers/../MyMembers/chiefs/
> > models.py", line 115, in save
> >    super(MemberProfile, self).save(self,*args,**kwargs)
> >  File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line
> > 410, in save
> >    self.save_base(force_insert=force_insert,
> > force_update=force_update)
> >  File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line
> > 483, in save_base
> >    values =
> >  File "/usr/lib/pymodules/python2.6/django/db/models/fields/
> > __init__.py", line 192, in get_db_prep_save
> >    return self.get_db_prep_value(value)
> >  File "/usr/lib/pymodules/python2.6/django/db/models/fields/
> > __init__.py", line 511, in get_db_prep_value
> >    return connection.ops.value_to_db_date(self.to_python(value))
> >  File "/usr/lib/pymodules/python2.6/django/db/models/fields/
> > __init__.py", line 472, in to_python
> >    if not ansi_date_re.search(value):
> > TypeError: expected string or buffer
>
> > m1 is an instance of MemberProfile...
>
> > The error goes away if I comment out everything to do with
> > DateField...
> > Am I doing something retarded? As far as I can see I'm populating the
> > fields the right way...
>
> > Any help would be greatly appreciated
>
> > --
> > 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<django-users%2bunsubscr...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/django-users?hl=en.

--
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 June 17, 2010 06:47:28

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

DateField issues


Your error is this...
def save(self,*args,**kwargs):
if not self.start_date:
self.start_date=models.DateField(datetime.date.today())
year=timedelta(days=365)
self.expire_date=models.DateField(datetime.date.today()
+year)



On Thu, Jun 17, 2010 at 8:40 AM, Sheena <sheena.oconn...@gmail.com> wrote:

> Thanks
>
> The question is how do I populate a field with some other date, for
> example, there's a date of birth field that the auto stuff wont be
> ideal for...
> In the populate method i mentioned before, i passed the dob(date of
> birth) field a Date object initialized to something arbitrary. Does
> the DateField not get along with standard date objects? What format
> should stuff be in for populating DateFields?
>
> On Jun 17, 7:24 am, Alexander Jeliuc <jeliucalexa...@gmail.com> wrote:
> > read about autofill_now=True and autofill=True
> >
> > On Thu, Jun 17, 2010 at 8:14 AM, Sheena <sheena.oconn...@gmail.com>
> wrote:
> > > I've defined a number of models, one of which I've called
> > > MemberProfile that looks like this
> >
> > > class MemberProfile(models.Model):
> > > postal_addr1=models.CharField(max_length=50, verbose_name="postal
> > > address line 1")
> > > postal_addr2=models.CharField(max_length=50, verbose_name="postal
> > > address line 2")
> > > postal_addr3=models.CharField(max_length=50, verbose_name="postal
> > > address line 3")
> > > postalcode=models.CharField(max_length=4, verbose_name="postal
> > > code")
> > > res_addr1=models.CharField(max_length=50,
> > > verbose_name="residential address line 1")
> > > res_addr2=models.CharField(max_length=50,
> > > verbose_name="residential address line 2")
> > > res_addr3=models.CharField(max_length=50,
> > > verbose_name="residential address line 3")
> > > rescode=models.CharField(max_length=4, verbose_name="residential
> > > postal code")
> > > homeno=models.CharField(max_length=12, verbose_name="home number")
> > > workno=models.CharField(max_length=12, verbose_name="work number")
> > > cellno=models.CharField(max_length=12, unique=True,
> > > verbose_name="cellphone number")
> > > idno=models.CharField(max_length=13, unique=True, verbose_name="id
> > > or passport number")
> > > occu=models.CharField(max_length=15, verbose_name="occupation")
> > > employer=models.CharField(max_length=30)
> > > amount_paid=models.IntegerField(blank=True, null=True,
> > > help_text="total fees paid to date", verbose_name="total reciepts")
> > > reciept_no=models.IntegerField(blank=True, null=True,
> > > help_text="latest reciept number", verbose_name="reciept number")
> > > dob=models.DateField(verbose_name="date of birth")
> > > start_date=models.DateField()
> > > expire_date=models.DateField()
> > > captured_by=models.CharField(max_length=50, help_text="name of
> > > data capturer")
> > > photo=models.ImageField(upload_to="memberphotos", null=True,
> > > blank=True)
> > > permission=models.CharField(max_length=1,
> > > choices=PERMISSION_CHOICES, help_text="Administration level, if the
> > > user is a superuser this field has no effect")
> > > user=models.ForeignKey(User,unique=True,related_name="member",
> > > editable=False)
> > > branch=models.ForeignKey(Branch,related_name="member", null=True)
> >
> > > def create_member(self,username,email,password):
> > > m=MemberProfile()
> >
> > >
> m.user=User.objects.create_user(username=username,email=email,password=password)
> > > return m
> >
> > > def __unicode__(self):
> > > return self.user.username
> >
> > > def save(self,*args,**kwargs):
> > > if not self.start_date:
> > > self.start_date=models.DateField(datetime.date.today())
> > > year=timedelta(days=365)
> > > self.expire_date=models.DateField(datetime.date.today()
> > > +year)
> > > super(MemberProfile, self).save(self,*args,**kwargs)
> >
> > > I then wrote a method called populate just to test the database out
> > > and it is pretty friendly until I get this:
> >
> > > Traceback (most recent call last):
> > > File "<console>", line 1, in <module>
> > > File "/host/Shared info/MyDBA/MyMembers/populate.py", line 114, in
> > > populate
> > > m1.save()
> > > File "/host/Shared info/MyDBA/MyMembers/../MyMembers/chiefs/
> > > models.py", line 115, in save
> > > super(MemberProfile, self).save(self,*args,**kwargs)
> > > File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line
> > > 410, in save
> > > self.save_base(force_insert=force_insert,
> > > force_update=force_update)
> > > File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line
> > > 483, in save_base
> > > values =
> > > File "/usr/lib/pymodules/python2.6/django/db/models/fields/
> > > __init__.py", line 192, in get_db_prep_save
> > > return self.get_db_prep_value(value)
> > > File "/usr/lib/pymodules/python2.6/django/db/models/fields/
> > > __init__.py", line 511, in get_db_prep_value
> > > return connection.ops.value_to_db_date(self.to_python(value))
> > > File "/usr/lib/pymodules/python2.6/django/db/models/fields/
> > > __init__.py", line 472, in to_python
> > > if not ansi_date_re.search(value):
> > > TypeError: expected string or buffer
> >
> > > m1 is an instance of MemberProfile...
> >
> > > The error goes away if I comment out everything to do with
> > > DateField...
> > > Am I doing something retarded? As far as I can see I'm populating the
> > > fields the right way...
> >
> > > Any help would be greatly appreciated
> >
> > > --
> > > 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<django-users%2bunsubscr...@googlegroups.com>
> <django-users%2bunsubscr...@googlegroups.com<django-users%252bunsubscr...@googlegroups.com>
> >
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/django-users?hl=en.
>
> --
> 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<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>
>

--
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 June 17, 2010 06:51:08

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

DateField issues


Here is DateField class
I think You will find all You need here


class DateField(Field):
description = _("Date (without time)")

empty_strings_allowed = False
default_error_messages = {
'invalid': _('Enter a valid date in YYYY-MM-DD format.'),
'invalid_date': _('Invalid date: %s'),
}
def __init__(self, verbose_name=None, name=None, auto_now=False,
auto_now_add=False, **kwargs):
self.auto_now, self.auto_now_add = auto_now, auto_now_add
#HACKs : auto_now_add/auto_now should be done as a default or a
pre_save.
if auto_now or auto_now_add:
kwargs = False
kwargs = True
Field.__init__(self, verbose_name, name, **kwargs)

def get_internal_type(self):
return "DateField"

def to_python(self, value):
if value is None:
return value
if isinstance(value, datetime.datetime):
return value.date()
if isinstance(value, datetime.date):
return value

if not ansi_date_re.search(value):
raise exceptions.ValidationError(self.error_messages)
# Now that we have the date string in YYYY-MM-DD format, check to
make
# sure it's a valid date.
# We could use time.strptime here and catch errors, but
datetime.date
# produces much friendlier error messages.
year, month, day = map(int, value.split('-'))
try:
return datetime.date(year, month, day)
except ValueError, e:
msg = self.error_messages % _(str(e))
raise exceptions.ValidationError(msg)

def pre_save(self, model_instance, add):
if self.auto_now or (self.auto_now_add and add):
value = datetime.datetime.now()
setattr(model_instance, self.attname, value)
return value
else:
return super(DateField, self).pre_save(model_instance, add)

def contribute_to_class(self, cls, name):
super(DateField,self).contribute_to_class(cls, name)
if not self.null:
setattr(cls, 'get_next_by_%s' % self.name,
curry(cls._get_next_or_previous_by_FIELD, field=self,
is_next=True))
setattr(cls, 'get_previous_by_%s' % self.name,
curry(cls._get_next_or_previous_by_FIELD, field=self,
is_next=False))

def get_prep_lookup(self, lookup_type, value):
# For "__month", "__day", and "__week_day" lookups, convert the
value
# to an int so the database backend always sees a consistent type.
if lookup_type in ('month', 'day', 'week_day'):
return int(value)
return super(DateField, self).get_prep_lookup(lookup_type, value)

def get_prep_value(self, value):
return self.to_python(value)

def get_db_prep_value(self, value, connection, prepared=False):
# Casts dates into the format expected by the backend
if not prepared:
value = self.get_prep_value(value)
return connection.ops.value_to_db_date(value)

def value_to_string(self, obj):
val = self._get_val_from_obj(obj)
if val is None:
data = ''
else:
data = datetime_safe.new_date(val).strftime("%Y-%m-%d")
return data

def formfield(self, **kwargs):
defaults = {'form_class': forms.DateField}
defaults.update(kwargs)
return super(DateField, self).formfield(**defaults)

On Thu, Jun 17, 2010 at 8:47 AM, Alexander Jeliuc
<jeliucalexa...@gmail.com>wrote:

> Your error is this...
> def save(self,*args,**kwargs):
> if not self.start_date:
> self.start_date=models.DateField(datetime.date.today())
> year=timedelta(days=365)
> self.expire_date=models.DateField(datetime.date.today()
> +year)
>
>
>
> On Thu, Jun 17, 2010 at 8:40 AM, Sheena <sheena.oconn...@gmail.com> wrote:
>
>> Thanks
>>
>> The question is how do I populate a field with some other date, for
>> example, there's a date of birth field that the auto stuff wont be
>> ideal for...
>> In the populate method i mentioned before, i passed the dob(date of
>> birth) field a Date object initialized to something arbitrary. Does
>> the DateField not get along with standard date objects? What format
>> should stuff be in for populating DateFields?
>>
>> On Jun 17, 7:24 am, Alexander Jeliuc <jeliucalexa...@gmail.com> wrote:
>> > read about autofill_now=True and autofill=True
>> >
>> > On Thu, Jun 17, 2010 at 8:14 AM, Sheena <sheena.oconn...@gmail.com>
>> wrote:
>> > > I've defined a number of models, one of which I've called
>> > > MemberProfile that looks like this
>> >
>> > > class MemberProfile(models.Model):
>> > > postal_addr1=models.CharField(max_length=50, verbose_name="postal
>> > > address line 1")
>> > > postal_addr2=models.CharField(max_length=50, verbose_name="postal
>> > > address line 2")
>> > > postal_addr3=models.CharField(max_length=50, verbose_name="postal
>> > > address line 3")
>> > > postalcode=models.CharField(max_length=4, verbose_name="postal
>> > > code")
>> > > res_addr1=models.CharField(max_length=50,
>> > > verbose_name="residential address line 1")
>> > > res_addr2=models.CharField(max_length=50,
>> > > verbose_name="residential address line 2")
>> > > res_addr3=models.CharField(max_length=50,
>> > > verbose_name="residential address line 3")
>> > > rescode=models.CharField(max_length=4, verbose_name="residential
>> > > postal code")
>> > > homeno=models.CharField(max_length=12, verbose_name="home number")
>> > > workno=models.CharField(max_length=12, verbose_name="work number")
>> > > cellno=models.CharField(max_length=12, unique=True,
>> > > verbose_name="cellphone number")
>> > > idno=models.CharField(max_length=13, unique=True, verbose_name="id
>> > > or passport number")
>> > > occu=models.CharField(max_length=15, verbose_name="occupation")
>> > > employer=models.CharField(max_length=30)
>> > > amount_paid=models.IntegerField(blank=True, null=True,
>> > > help_text="total fees paid to date", verbose_name="total reciepts")
>> > > reciept_no=models.IntegerField(blank=True, null=True,
>> > > help_text="latest reciept number", verbose_name="reciept number")
>> > > dob=models.DateField(verbose_name="date of birth")
>> > > start_date=models.DateField()
>> > > expire_date=models.DateField()
>> > > captured_by=models.CharField(max_length=50, help_text="name of
>> > > data capturer")
>> > > photo=models.ImageField(upload_to="memberphotos", null=True,
>> > > blank=True)
>> > > permission=models.CharField(max_length=1,
>> > > choices=PERMISSION_CHOICES, help_text="Administration level, if the
>> > > user is a superuser this field has no effect")
>> > > user=models.ForeignKey(User,unique=True,related_name="member",
>> > > editable=False)
>> > > branch=models.ForeignKey(Branch,related_name="member", null=True)
>> >
>> > > def create_member(self,username,email,password):
>> > > m=MemberProfile()
>> >
>> > >
>> m.user=User.objects.create_user(username=username,email=email,password=password)
>> > > return m
>> >
>> > > def __unicode__(self):
>> > > return self.user.username
>> >
>> > > def save(self,*args,**kwargs):
>> > > if not self.start_date:
>> > > self.start_date=models.DateField(datetime.date.today())
>> > > year=timedelta(days=365)
>> > > self.expire_date=models.DateField(datetime.date.today()
>> > > +year)
>> > > super(MemberProfile, self).save(self,*args,**kwargs)
>> >
>> > > I then wrote a method called populate just to test the database out
>> > > and it is pretty friendly until I get this:
>> >
>> > > Traceback (most recent call last):
>> > > File "<console>", line 1, in <module>
>> > > File "/host/Shared info/MyDBA/MyMembers/populate.py", line 114, in
>> > > populate
>> > > m1.save()
>> > > File "/host/Shared info/MyDBA/MyMembers/../MyMembers/chiefs/
>> > > models.py", line 115, in save
>> > > super(MemberProfile, self).save(self,*args,**kwargs)
>> > > File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line
>> > > 410, in save
>> > > self.save_base(force_insert=force_insert,
>> > > force_update=force_update)
>> > > File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line
>> > > 483, in save_base
>> > > values =
>> > > File "/usr/lib/pymodules/python2.6/django/db/models/fields/
>> > > __init__.py", line 192, in get_db_prep_save
>> > > return self.get_db_prep_value(value)
>> > > File "/usr/lib/pymodules/python2.6/django/db/models/fields/
>> > > __init__.py", line 511, in get_db_prep_value
>> > > return connection.ops.value_to_db_date(self.to_python(value))
>> > > File "/usr/lib/pymodules/python2.6/django/db/models/fields/
>> > > __init__.py", line 472, in to_python
>> > > if not ansi_date_re.search(value):
>> > > TypeError: expected string or buffer
>> >
>> > > m1 is an instance of MemberProfile...
>> >
>> > > The error goes away if I comment out everything to do with
>> > > DateField...
>> > > Am I doing something retarded? As far as I can see I'm populating the
>> > > fields the right way...
>> >
>> > > Any help would be greatly appreciated
>> >
>> > > --
>> > > 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<django-users%2bunsubscr...@googlegroups.com>
>> <django-users%2bunsubscr...@googlegroups.com<django-users%252bunsubscr...@googlegroups.com>
>> >
>> > > .
>> > > For more options, visit this group at
>> > >http://groups.google.com/group/django-users?hl=en.
>>
>> --
>> 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<django-users%2bunsubscr...@googlegroups.com>
>> .
>> For more options, visit this group at
>>http://groups.google.com/group/django-users?hl=en.
>>
>>
>

--
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

#6 June 17, 2010 09:03:14

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

DateField issues


On Jun 17, 6:40 am, Sheena <sheena.oconn...@gmail.com> wrote:
> Thanks
>
> The question is how do I populate a field with some other date, for
> example, there's a date of birth field that the auto stuff wont be
> ideal for...
> In the populate method i mentioned before, i passed the dob(date of
> birth) field a Date object initialized to something arbitrary. Does
> the DateField not get along with standard date objects? What format
> should stuff be in for populating DateFields?

For some reason you're trying to set the value of your date fields to
a *field*. Just as with any other field, you need to set the value of
the field to the corresponding value - for a charfield, you would set
it to a string:
self.mycharfield = "mystring"
and for an integer you would do:
self.myintegerfield = 1
so for a date field you just set it to a date:
self.mydatefield = datetime.date.today()

--
DR.

--
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 19th of October
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