19👍
You can’t do
LivingRoom.objects.create(user=instance)
because you have an __init__
method that does NOT take user
as argument.
You need something like
#signal function: if a user is created, add control livingroom to the user
def create_control_livingroom(sender, instance, created, **kwargs):
if created:
my_room = LivingRoom()
my_room.user = instance
Update
But, as bruno has already said it, Django’s models.Model
subclass’s initializer is best left alone, or should accept *args
and **kwargs
matching the model’s meta fields.
So, following better principles, you should probably have something like
class LivingRoom(models.Model):
'''Living Room object'''
user = models.OneToOneField(User)
def __init__(self, *args, temp=65, **kwargs):
self.temp = temp
return super().__init__(*args, **kwargs)
Note – If you weren’t using temp
as a keyword argument, e.g. LivingRoom(65)
, then you’ll have to start doing that. LivingRoom(user=instance, temp=66)
or if you want the default (65), simply LivingRoom(user=instance)
would do.
13👍
I got the same error.
On my view I was overriding get_form_kwargs() like this:
class UserAccountView(FormView):
form_class = UserAccountForm
success_url = '/'
template_name = 'user_account/user-account.html'
def get_form_kwargs(self):
kwargs = super(UserAccountView, self).get_form_kwargs()
kwargs.update({'user': self.request.user})
return kwargs
But on my form I failed to override the init() method. Once I did it. Problem solved
class UserAccountForm(forms.Form):
first_name = forms.CharField(label='Your first name', max_length=30)
last_name = forms.CharField(label='Your last name', max_length=30)
email = forms.EmailField(max_length=75)
def __init__(self, *args, **kwargs):
user = kwargs.pop('user')
super(UserAccountForm, self).__init__(*args, **kwargs)
- [Django]-Customize/remove Django select box blank option
- [Django]-Django Rest Framework partial update
- [Django]-What does 'many = True' do in Django Rest FrameWork?
5👍
LivingRoom.objects.create()
calls LivingRoom.__init__()
– as you might have noticed if you had read the traceback – passing it the same arguments. To make a long story short, a Django models.Model
subclass’s initializer is best left alone, or should accept *args and **kwargs matching the model’s meta fields. The correct way to provide default values for fields is in the field constructor using the default
keyword as explained in the FineManual.
- [Django]-Django template filters, tags, simple_tags, and inclusion_tags
- [Django]-Using Basic HTTP access authentication in Django testing framework
- [Django]-Docker image error: "/bin/sh: 1: [python,: not found"
2👍
Check your imports. There could be two classes with the same name. Either from your code or from a library you are using. Personally that was the issue.
- [Django]-Add a non-model field on a ModelSerializer in DRF 3
- [Django]-Django: How should I store a money value?
- [Django]-Django ALLOWED_HOSTS IPs range
- [Django]-Django override save for model only in some cases?
- [Django]-How to add multiple objects to ManyToMany relationship at once in Django ?
- [Django]-How to fix error "ERROR: Command errored out with exit status 1: python." when trying to install django-heroku using pip