Monday, December 7, 2009

Re: model design problem

Each port would have about a half-dozen fields, and different port types are totally different from one another.  I won't be querying port fields.

I've thought about your JSON field trick before, but not with the nice wrapper methods.  Good idea, thanks!

The solution does have flaws, but if nothing better comes up I might just go this way.

Thanks again,
Dan.


On Mon, Dec 7, 2009 at 9:20 AM, Shawn Milochik <shawn@milochik.com> wrote:
What kind of information will the database have to store about the ports themselves?

How will you need to be able to filter querysets by information in the port fields, if at all?

I'm going to throw this one solution out there, but depending on your needs it may not fit:

Create a red_ports column and a green_ports column in your model. Each one will contain serialized JSON. You can just dump a Python dictionary into those fields with simplejson.dumps, and retrieve the data with simplejson.loads. Then, you add methods to your model which you can use in the views that deal with the model.





Here are a couple of lines of code from one of my models. I'm storing the hashes of old passwords to prevent people from re-using passwords.

#field definition in model
prev_passwords = models.TextField(default = '{}')

I then get and set the values like this:

   def _get_prev_passwords(self):
       return simplejson.loads(self.prev_passwords)

   def _set_prev_passwords(self, pw_dict):
       self.prev_passwords = simplejson.dumps(pw_dict)

   previous_passwords = property(_get_prev_passwords, _set_prev_passwords)

This way, my model's .previous_passwords attribute is available in my views, and the serialization is transparent.




So, your Python dictionaries can contain all kinds of data about the red and green ports, and contain entries for any number of ports. Your models remain clean.
The biggest flaw in this method is that, if you need to select a set of products based on an attribute of a port, you'll have to jump through hoops to make it happen.

Shawn


--

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.





--
Daniel Goertzen
-----------------
dang@networkintegritysystems.com (work)
daniel.goertzen@gmail.com (home)
-----------------
1 204 272 6149 (home/office)
1 204 470 8360 (mobile)
-----------------



--

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate