简体   繁体   中英

How do I activate/use django-mptt on an database with data?

I have an existing database with an offices table. It has the following fields:

id
lvl
parent_id
name
code

I have a Django (1.7) model that looks like:

class Office(models.Model):
    id          = models.IntegerField( primary_key=True )
    lvl         = models.IntegerField( blank=True, null=False )
    parent      = models.ForeignKey( 'self', null=True)
    name        = models.TextField( blank=True)
    code        = models.TextField( blank=True)

    class Meta:
        managed  = False
        db_table = 'offices'

    def __str__(self):
        return "(" + self.code + ") " + self.name + ", level " + str(self.lvl)

In its current form, calling someoffice.parent works properly and returns the parent. However, I need to be able to call someoffice.children() and get back the children of an individual parent node.

This seems like a good use case for django-mptt . However, I can't figure out how to use django-mptt on the existing model and database table without Django throwing errors about missing migrations. I'm more than happy to scrap the model and/or replace it with something else. What I do not want to do is to toss out the existing table, unless I can easily do it once and not have to struggle with this anymore.

How can I work with the existing database table to implement django-mptt ?


Edit : There are several other convenience functions in django-mptt I'd also would like to use, so I'm looking for advice on how to either implement django-mptt with the existing table, to modify the table in situ so that I can use django-mptt without migrations, or to create a new table and then load my data into it. Ideally, I can alter the existing table and then use django-mptt without needing to run a Django migration.

There's no need for mptt if you just want to get children for a parent. That functionality is built in to Django: given an instance of Office called my_office , you can get its children with my_office.office_set.all() .

You probably want to give the reverse accessor a better name, which you can do by modifying the field definition:

parent      = models.ForeignKey('self', null=True, related_name='children')

Now you can do my_office.children.all() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM