views:

46

answers:

2

Suppose I have a model:

class SomeModel(models.Model):
   id = models.AutoField(primary_key=True)
   a = models.IntegerField(max_length=10)
   b = models.CharField(max_length=7)

Currently I am using the default admin to create/edit objects of this type. How do I set the field 'a' to have the same number as id? (default=???)



Other question

Suppose I have a model:

   event_date = models.DateTimeField( null=True)
      year = models.IntegerField( null=True)
      month = models.CharField(max_length=50, null=True)
      day = models.IntegerField( null=True)

How can i set the year, month and day fields by default to be the same as event_date field?

+1  A: 

You can override the save() method, and if a is still empty/null at that point, copy the id field. Same goes for your 2nd question.

Mark
every time I put editable=False it doesn't put the value. The field has to be visible in order to change it.is there a way to hide the field and still change the value?
Daniel Garcia
A: 

does it HAVE to be a field stored in the database? (redundancy is bad yaddayadda)

If you just want to call it from inside python, use this:

event_date = models.DateTimeField( null=True)

@property
def year(self):
    return self.event_date and self.event_date.strftime("%Y") or None
mawimawi
this works. Thanks! Also every time I put editable=False it doesn't put the value. The field has to be visible in order to change it. is there a way to hide the field and still change the value?
Daniel Garcia