1

I am getting below error when I use auto_now_add in my Model Form.

TypeError: __init__() got an unexpected keyword argument 'auto_now_add'

Here is my model field

modified = models.DateTimeField(blank = True)

Declaration in form. I have seen in one of the posts DateTimeField Not Working to add initial = datetime.datetime.now for auto populating

import datetime

modified = forms.DateTimeField(initial = datetime.datetime.now) - When I use this no error is coming but datetime was not auto populating.

I have used the same in self.fields['modified'] - Still no use

Any of the above statements were not working. Some one help me on this.


I am pasting all my model class and Model Form here Model Class

class Users(models.Model):
  name = models.CharField(max_length = 100)
  role = models.ForeignKey(RolesConfig, db_column = 'role')
  level = models.ForeignKey(LevelConfig, db_column = 'level')
  team_name = models.ForeignKey(TeamNamesConfig, db_column = 'team_name')
  location = models.ForeignKey(LocationConfig, db_column = 'location')
  modified = models.DateTimeField(blank = True)
  class Meta:
     db_table = u'users'
     def __str__(self):
            return "%s" % (self.ldap)
     def __unicode__(self):
         return u'%s' % (self.ldap)

I have modified the field in phpmyadmin

Modified field structure

This is my ModelForm

class TargetForm(forms.ModelForm):
    modified = forms DateTimeField(initial = datetime.datetime.now)
    def __init__(self, *args, **kwargs):
       super(MMPodTargetForm, self).__init__(*args, **kwargs)    
       self.fields['modified'] = forms.DateTimeField(initial = datetime.datetime.now)
    class Meta:
       model = models.Users

I need to get current date and time autopopulated in the form, when the form loads. Tell me whats wrong in my code.

Community
  • 1
  • 1
vkrams
  • 7,267
  • 17
  • 79
  • 129
  • How would your DateTimeField definition look like with `auto_now_add` set? – Torsten Engelbrecht Jun 10 '11 at 03:55
  • I have used like this `modified = models.DateTimeField(auto_now_add = True)`. When I give this it says unexpected keyword argument 'auto_now_add – vkrams Jun 10 '11 at 04:13
  • The thing is that when using forms and `auto_now_add` the `DateTimeField` should be not editable in the form (Django docs: "As currently implemented, setting auto_now or auto_now_add to True will cause the field to have editable=False and blank=True set."). Where exactly do you get this error? When trying to call the form or somewhere else? – Torsten Engelbrecht Jun 10 '11 at 04:20
  • I have tried all combinations.... Its not working. – vkrams Jun 10 '11 at 05:56
  • Or Just give me the way to create a datetime field in model and how to set initial value as `datetime.datetime.now()` in model form. I am trying from one hour and i'm not getting – vkrams Jun 10 '11 at 05:58
  • no it didn't worked. I set `modified = models.DateTimeField(blank = True, default = datetime.datetime.now())` – vkrams Jun 10 '11 at 06:28
  • Hi torston i have modified the post with Model and Modelform data. Could you please have a look? – vkrams Jun 10 '11 at 06:37
  • `datetime.datetime.now` is a function and it should be `datetime.datetime.now()`. – Torsten Engelbrecht Jun 10 '11 at 07:15

1 Answers1

2

I think the error is because you're adding the auto_now_add extra argument to your form instead of to your mode. Try changing your model to the following to see if that fixes the problem (untested):

class Users(models.Model):
  name = models.CharField(max_length = 100)
  role = models.ForeignKey(RolesConfig, db_column = 'role')
  level = models.ForeignKey(LevelConfig, db_column = 'level')
  team_name = models.ForeignKey(TeamNamesConfig, db_column = 'team_name')
  location = models.ForeignKey(LocationConfig, db_column = 'location')
  modified = models.DateTimeField(auto_now = True)
  class Meta:
     db_table = u'users'
     def __str__(self):
            return "%s" % (self.ldap)
     def __unicode__(self):
     return u'%s' % (self.ldap)
vkrams
  • 7,267
  • 17
  • 79
  • 129
rolling stone
  • 12,668
  • 9
  • 45
  • 63
  • hey maz it works fine thank you. But when i submit my form datetime is not saving. Could you please also help on this – vkrams Jun 10 '11 at 11:24
  • hey vikram, make sure you are importing the datetime module on your forms.py page, and then set the default to datetime.datetime.now() Let me know if that works. – rolling stone Jun 10 '11 at 16:39
  • Hey maz after removing auto_now_add = True it worked. Thanks for the help – vkrams Jun 13 '11 at 03:16