2

I have a php app that I am considering rewriting in either Django or Rails (have done some maitence work over the years but not that familiar with issues like this). Ideally, I'd like to db schema as close as possible to what I'm using. It has a model this is like the following:

menu - id, name
menu_headers - id, menu_id, parent_menu_header_id, sort, name

The logic in the getMenu($id) function is to get the menu by the id and then get the menu_headers with corrent menu_id and a parent_menu_header_id of 0. There is a sub-menu function that gets called that gets submenus based upon the parent_menu_header_id. In other words, 0 means it is a root menu_header (ie select * from menu_headers where menu_id=$menu_id and parent_menu_header_id=0 order by sort). This all gets pushed to memcache so performance is not a concern.

I'm considering moving the app to django and am investigating how difficult / possible this would be.

I currently have:

class Menu(models.Model):
    location=models.ForeignKey(Location)
    name = models.CharField(max_length=200)

class Menu_Header(models.Model):
    menu=models.ForeignKey(Menu)
    parent=models.ForeignKey('self',null=True,blank=True,related_name="children")

A couple of issues have come up. It isn't a true foreign key relationship. It looks like composite foreign keys are not supported. Maybe using something like a Root_Menu_Header which does have a true fk relationship. Is there a better way to model this? I have looked at the django-mptt but think that this should be possible. Any ideas?

thx

--edit #2

I'm probably not getting what you're saying but for example I currently have:

menu   
id    name  
1     test menu  

menu_header  
id  menu_id parent_id   name  
1   1       NULL        Wine  
2   1       1           Red  
3   1       1           White  

When I get the Menu object, it has all 3 menu headers at the same level. So this clearly isn't working correctly. Should I be manipulating this at the view level then? Or should the foreign key (menu_id) not be set in the menu_header table? Sorry for confusion but will be a lot of help to figure this out. If any suggestions on whether better to do this in Rails, that would also be appreciated.

thx

timpone
  • 19,235
  • 36
  • 121
  • 211
  • If your menu is a tree structure then you definitely should look at django-mptt. It's pretty easy to use and you'll have all necessary methods to build the menu structure. BTW, use CamelCase in class names, e.g. `class MenuHeader`. See http://www.python.org/dev/peps/pep-0008/ – Andrey Fedoseev May 26 '11 at 17:50
  • i tried installing last night since it looked ok - are you using 4.2 or the 5.0 alpha. The current build has a requirement for TreeForeignKey which exists only in the v5pre http://stackoverflow.com/questions/5781352/django-mptt-importerror – timpone May 26 '11 at 19:39
  • cool - I got this piece working and updated the class names. Do you use the MPTTModelAdmin piece? thx – timpone May 26 '11 at 21:00

1 Answers1

0

Django expects that if a foreign key is not NULL, that it maps up to a real object. You're not really going to be able to get around that. However, the absence of a value for parent (NULL) implicitly implies there is no parent, and you'll find that developing the app around this will be quite natural.

The only real problem I see is if you're trying to use the existing database (or migrating data there from). In which case, you'll only need to run a SQL update and set parent to NULL wherever it's 0.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • hmm.. I've added an update in the question under edit #2. I am pretty sure this can be done and is just a lack of understanding on my part. thx for help. – timpone May 26 '11 at 19:48
  • Yes, your queryset is always going to be flat. Django's not going to create some hierarchical list of lists for you. That's logic you'll need to take care of, preferably in the model (business logic belongs in the model). – Chris Pratt May 31 '11 at 19:01
  • I don't get it, doesn't this contradict? "expects that if a foreign key is not NULL" but "the absence of a value for parent (NULL) implicitly implies there is no parent". – citynorman Mar 10 '19 at 04:33