Code

Opened 5 years ago

Closed 5 years ago

#10247 closed (invalid)

problem with model constructor override

Reported by: azymnis Owned by: nobody
Component: Database layer (models, ORM) Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I am in a situation where I want to create a model instance from another object. For this reason, I am trying to override the Model constructor so that it can take an object as an argument. However, this does not seem to work properly, since after I save, I cannot access the data with the object manager. Here is an example. Suppose my models.py code is:

from django.db import models

class Person(models.Model):
     first_name = models.CharField(max_length=40)
     last_name = models.CharField(max_length=40)
 
     def __init__(self,datadict):
         models.Model.__init__(self,
             first_name = datadict['first_name'],
             last_name = datadict['last_name'])

Then, when this is what happens when I use this:

>>>from cddbase.models import Person
>>>data_dict = {'first_name': 'Rob', 'last_name': 'Smith'}
>>>person = Person(data_dict)
>>>person.save()
>>>Person.objects.all()
[]

So basically the Person.save() method is not working... However, the Person class is created correctly. One thing that I noticed is that the SQL seems to be wrong. It issues and UPDATE rather than an INSERT command, even though the row in the table does not exist.

Any ideas what I can do?

Thanks,
Argyris

Attachments (0)

Change History (1)

comment:1 Changed 5 years ago by ubernostrum

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

Usage questions should go to the django-users list

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.