Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » turning a Model instance into an instance of one of it's subclasses [RSS Feed]

#1 Dec. 20, 2010 20:04:04

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

turning a Model instance into an instance of one of it's subclasses


Hello all,

I was wonder if there is a way to turn a model instance into an
instance of a subclass of it's class. E.g.:

class Document(models.Model):
doctype = models.CharField(max_length=256, blank=True, null=True)

class UploadedFile(Document):
filename = models.CharField(max_length=256)

Given a Document that isn't already an UploadedFile, can I turn it
into one? I know at the database level it's just a matter of inserting
a new row into the uploadedfile table with document_ptr_id = the
Document's id. I've tried

doc = Document.objects.create(doctype='photo')
uf = UploadedFile(
document_ptr=doc,
filename='test.jpg',
)
uf.save()

But that seems to produce two separate instances (i.e. uf.doctype !=
doc.doctype).

Is there a way to do this?

Thanks in advance.
-Morgan Wahl

--
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 Dec. 20, 2010 21:50:22

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

turning a Model instance into an instance of one of it's subclasses


On Mon, Dec 20, 2010 at 8:29 PM, morgan wahl <morgy.w...@gmail.com> wrote:

> Hello all,
>
> I was wonder if there is a way to turn a model instance into an
> instance of a subclass of it's class. E.g.:
>
> class Document(models.Model):
> doctype = models.CharField(max_length=256, blank=True, null=True)
>
> class UploadedFile(Document):
> filename = models.CharField(max_length=256)
>
> Given a Document that isn't already an UploadedFile, can I turn it
> into one? I know at the database level it's just a matter of inserting
> a new row into the uploadedfile table with document_ptr_id = the
> Document's id. I've tried
>
>
I think that something like this should do the work:

Document.UploadFile.filename="rat.doc"
Document.save()


--
Marc

--
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 Dec. 20, 2010 22:01:05

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

turning a Model instance into an instance of one of it's subclasses


On Mon, Dec 20, 2010 at 4:49 PM, Marc Aymerich <glicer...@gmail.com> wrote:
>
>
> On Mon, Dec 20, 2010 at 8:29 PM, morgan wahl <morgy.w...@gmail.com> wrote:
>>
>> Hello all,
>>
>> I was wonder if there is a way to turn a model instance into an
>> instance of a subclass of it's class. E.g.:
>>
>> class Document(models.Model):
>>    doctype = models.CharField(max_length=256, blank=True, null=True)
>>
>> class UploadedFile(Document):
>>    filename = models.CharField(max_length=256)
>>
>> Given a Document that isn't already an UploadedFile, can I turn it
>> into one? I know at the database level it's just a matter of inserting
>> a new row into the uploadedfile table with document_ptr_id = the
>> Document's id. I've tried
>>
>
> I think that something like this should do the work:
> Document.UploadFile.filename="rat.doc"
> Document.save()
>
> --
> Marc
Um, I'm not sure what it is you're trying to demonstrate here... in my
example bits of code Document and UploadedFile are Model classes, not
instances.

>
> --
> 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 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 Dec. 20, 2010 22:04:36

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

turning a Model instance into an instance of one of it's subclasses


On Mon, Dec 20, 2010 at 11:00 PM, Morgan Wahl <morgy.w...@gmail.com> wrote:

> On Mon, Dec 20, 2010 at 4:49 PM, Marc Aymerich <glicer...@gmail.com>
> wrote:
> >
> >
> > On Mon, Dec 20, 2010 at 8:29 PM, morgan wahl <morgy.w...@gmail.com>
> wrote:
> >>
> >> Hello all,
> >>
> >> I was wonder if there is a way to turn a model instance into an
> >> instance of a subclass of it's class. E.g.:
> >>
> >> class Document(models.Model):
> >> doctype = models.CharField(max_length=256, blank=True, null=True)
> >>
> >> class UploadedFile(Document):
> >> filename = models.CharField(max_length=256)
> >>
> >> Given a Document that isn't already an UploadedFile, can I turn it
> >> into one? I know at the database level it's just a matter of inserting
> >> a new row into the uploadedfile table with document_ptr_id = the
> >> Document's id. I've tried
> >>
> >
> > I think that something like this should do the work:
> > Document.UploadFile.filename="rat.doc"
> > Document.save()
> >
> > --
> > Marc
> Um, I'm not sure what it is you're trying to demonstrate here... in my
> example bits of code Document and UploadedFile are Model classes, not
> instances.
>
>
Hi Morgan, sorry, Yes you're rigth,


d=Document(doctype='whatever')
d.save()
u=UploadFile(document_ptr=d, filename='rat.doc')
u.save()


sorry for the confusion here :S

--
Marc

--
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 Dec. 20, 2010 22:14:11

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

turning a Model instance into an instance of one of it's subclasses


Yes, I had hope that would work, but it doesn't (see my original post). In
your example u.doctype would end up as None instead of 'whatever'.

--
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 Dec. 20, 2010 22:32:06

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

turning a Model instance into an instance of one of it's subclasses


Yes, I had hoped that would work, but it doesn't (see my original post). In
your example u.doctype would end up as None instead of 'whatever' (however,
u.document_ptr is d).

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

#7 Dec. 20, 2010 22:34:43

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

turning a Model instance into an instance of one of it's subclasses


On Mon, Dec 20, 2010 at 11:14 PM, morgan wahl <morgy.w...@gmail.com> wrote:

> Yes, I had hope that would work, but it doesn't (see my original post). In
> your example u.doctype would end up as None instead of 'whatever'.


woww, I really need go to sleep :)

I don't know the best way to achieve that, btw you can pass the document
attributes to the uploadfile constructor.

d=Document(doctype='whatever')
d.save()
u=UploadFile(document_ptr=d, doctype=d.doctype, filename='rat.doc')
u.save()



--
Marc

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

#8 Dec. 20, 2010 22:41:24

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

turning a Model instance into an instance of one of it's subclasses


Hmmm, I seem to have somewhat figured out what's going on.

Using Marc's example, when you instantiate u it's doctype field is set
to the default for Documents (None), since it isn't passed in. Then
when you save it, it overwrites the field in d.

So it seems the answer to my question is something like:

d = Document(doctype='whatever')
d.save()
u_kwargs = <dictionary of the fields of d>
u_kwargs.update({
'document_ptr_id': d,
'filename': 'rat.doc',
})
u = UploadedFile(**u_kwargs)
u.save()

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

#9 Dec. 20, 2010 22:42:02

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

turning a Model instance into an instance of one of it's subclasses


On Mon, Dec 20, 2010 at 11:34 PM, Marc Aymerich <glicer...@gmail.com> wrote:

>
>
> On Mon, Dec 20, 2010 at 11:14 PM, morgan wahl <morgy.w...@gmail.com>wrote:
>
>> Yes, I had hope that would work, but it doesn't (see my original post). In
>> your example u.doctype would end up as None instead of 'whatever'.
>
>
> woww, I really need go to sleep :)
>
> I don't know the best way to achieve that, btw you can pass the document
> attributes to the uploadfile constructor.
>
> d=Document(doctype='whatever')
> d.save()
> u=UploadFile(document_ptr=d, doctype=d.doctype, filename='rat.doc')
> u.save()
>
>
>
Morgan, take a look at the inheritanceManager of this app:https://github.com/carljm/django-model-utils#readme--
Marc

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

#10 Dec. 20, 2010 22:42:52

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

turning a Model instance into an instance of one of it's subclasses


ah, yes you beat me to it while I was composing my post below...

Thanks!

On Dec 20, 5:34 pm, Marc Aymerich <glicer...@gmail.com> wrote:
> On Mon, Dec 20, 2010 at 11:14 PM, morgan wahl <morgy.w...@gmail.com> wrote:
> > Yes, I had hope that would work, but it doesn't (see my original post). In
> > your example u.doctype would end up as None instead of 'whatever'.
>
> woww, I really need go to sleep :)
>
> I don't know the best way to achieve that, btw you can pass the document
> attributes to the uploadfile constructor.
>
> d=Document(doctype='whatever')
> d.save()
> u=UploadFile(document_ptr=d, doctype=d.doctype, filename='rat.doc')
> u.save()
>
> --
> Marc

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

  • Root
  • » Django
  • » turning a Model instance into an instance of one of it's subclasses [RSS Feed]

Board footer

Moderator control

Enjoy the 21st 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