开发者

django self referential models, use value of 0 or null as root for children table

开发者 https://www.devze.com 2023-03-08 10:32 出处:网络
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

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 g开发者_Go百科ets 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号