Friday 15 April 2011

python - Using Property to duck type two models -


I have a dynamic model that can have one of two objects through its related foreign key (call them Click on object1 and object2 ). Two other classes are functionally nearly identical, but this involves a little different information, on the suggestion of another Stack Overflow question, I am setting up Python method property () and which I am also receiving the object that exists. It looks like the following:

  class master (models.Model): object 1 = model. Exotic (Object 1, Blank = True) object2 = Model Forwardkey (object 2, empty = true) def get_object (self): if self.object2_id: return self.object2 else: return self.object1 def set_object (self, example): if isinstance (example, object 2): self.object2 = Example self.object1 = None else: self.object2 = None self.object1 = Example Object instance = Property (get_object, set_object)   

This is a simplified version of the class. I thought everything was working properly. I was able to set, and was able to get the information, and could display data organized in objectInstance on many of my pages. I am using django-tables2 to display information in tables, and it Was also able to show all the information. However, when I try to solve the data (using the provided convenient arrows) I get the field error:

  on field error / workflow / list / request / keyword u_objectInstance The farm can not be solved. The options are: Object 1, Object 2   

What is happening with any idea? Or what is the code snippet for you to see what is the reason?

EDIT:

It seems that I am not the only one with this problem. This post seems to indicate that it is a problem with django-tables2

It seems that when the table can display information placed in a property It has trouble troubleshooting

If you want to use a decorator, see.

But the best way

Edit

It may be that:

  class Master (models.Model): content_type = models.ForeignKey (ContentType) object_id = models.PositiveIntegerField () objectInstance = generic.GenericForeignKey ('content_type', 'object_id')   

Therefore, you You can do this:

  & gt; & Gt; & Gt; T = Master (object instance = object2 ()) & gt; & Gt; & Gt; T.save ()> gt; & Gt; & Gt; T.objectInstance & lt; Object2: a_description & gt; & Gt; & Gt; & Gt; T.objectInstance = Object1 ()> gt; & Gt; & Gt; T.save ()> gt; & Gt; & Gt; T.objectInstance & lt; Object1: a_description & gt;   

Hope it helps!

No comments:

Post a Comment