django-mptt

How to add django-mptt rebuild to migration?

点点圈 提交于 2019-12-12 12:44:09
问题 I have add the django-mptt to existing database, and create the new migration. Migration process was asked for default values for level , left , right and such fields, but doesn't add the model.rebuild operation to migration file. How to add rebuild operation to migration file manually? 回答1: Try the following: from __future__ import unicode_literals from django.db import migrations from mptt import register, managers def rebuild_tree(apps, schema_editor): YourMPTTModel = apps.get_model('your

How do I filter for all descendants of a ForeignKey MPTT Model?

偶尔善良 提交于 2019-12-12 10:06:27
问题 Here is the situation. I'm utilizing an MPTT Model in Django to create a hierarchy of music genres (Rock, Hard Rock, etc). I'm assigning one of the nodes of this hierarchy to an Album. Let's say I create a Album object with Hard Rock genre. How can I query my Albums for all Rock albums and have it include Rock and all descendants of the Rock genre? class Genre(MPTTModel): name = models.CharField(max_length=50, unique=True) parent = TreeForeignKey('self', null=True, blank=True, related_name=

Problem using django mptt

眉间皱痕 提交于 2019-12-11 05:32:16
问题 I am having problem implementing django mptt. Here is my model: class Company(models.Model): name = models.CharField( max_length=100) parent = models.ForeignKey('self', null=True, blank=True, related_name='children') mptt.register(Company, order_insertion_by=['name']) And class Financials(models.Model): company = models.ForeignKey(Company, related_name="financials") year = models.IntegerField() revenue = models.DecimalField(max_digits = 10, decimal_places = 2) So what I am looking at is how

How to validate and create related objects together with forms

和自甴很熟 提交于 2019-12-10 22:14:51
问题 I am trying to validate a related object (ForeignKey) when creating an object the base object with forms. The related object may or may not exist. Below I use MPTT but this is a general foreign key problem. I have a model like this: # model: class MyMPTTModel(models.Model): name = models.CharField(max_length=256, unique=True) # this is set parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children') #form class MyMPTTModelForm(forms.ModelForm):

Django-CMS warning on django-mptt

自古美人都是妖i 提交于 2019-12-10 19:14:09
问题 I have a Django-CMS 2.4.1 project that always gives me the warning: DeprecationWarning: Implicit manager CMSPlugin.tree will be removed in django-mptt 0.6. Explicitly define a TreeManager() on your model to remove this warning. This only occurs in production - not in dev. I tried: ./manage.py cms fix-mptt which gives me the output (after the same warning as above): fixing mptt page tree fixing mptt plugin tree all done But... this does not solve the problem, e.g. if I repeat the command it

How to order django-mptt tree by DateTimeField?

喜你入骨 提交于 2019-12-10 14:54:10
问题 This is the model I am using: class Comment(MPTTModel): comment = models.CharField(max_length=1023) resource = models.ForeignKey('Resource') created_at = models.DateTimeField(auto_now_add=True) parent = TreeForeignKey('self', null=True, blank=True, related_name='children') author = models.ForeignKey(User) class MPTTMeta: order_insertion_by = ['created_at'] However, when I try to add a comment from the admin site I get: ValueError at /admin/app/comment/add/ Cannot use None as a query value Am

Django-MPTT, how to

情到浓时终转凉″ 提交于 2019-12-10 14:16:21
问题 Hey, I have just installed the django-mptt lib, but i don't know how to get it to work :( I have added from mptt.models import MPTTModel class Category(MPTTModel): slug = models.SlugField(max_length=200, unique=True) name = models.CharField(max_length=100) parent = models.ForeignKey('self', blank=True, null=True, related_name='child') It that works fine - But when i go to the Django Admin page of my site i got an error: TemplateDoesNotExist at /admin/search/category/ admin/mptt_change_list

Why this database migration error after I upgrade my version django-mptt?

落花浮王杯 提交于 2019-12-10 12:31:15
问题 My Django application has a requirements.txt file (shown here) that I use to install modules in my virtual environment. Everything works fine. However, I'm now trying to upgrade django-mptt from 0.6.1 to the latest version. (I actually don't care to upgrade django-mptt. I just want to upgrade my version of Django. But it seems to upgrade Django, I must first upgrade django-mptt as described here). So I do pip install -U django-mptt . This causes django-mptt to go from 0.6.1 to 0.7.4 and

Use field value in limit_choices_to in Django

一笑奈何 提交于 2019-12-10 09:37:05
问题 I have two models Project and Group . My groups belong to a specific project. My groups have the fields project = ForeignKey(Project) and parent = ForeignKey('self') . Can I use limit_choices_to to make sure the options in foreign key parent only consist of groups inside the same project? I'm thinking of something like def limit_choices_to(self): return {'project': self.project} 回答1: This is impossible to do at the model level but you can change the queryset for this field in the form's

How do I add a trailing slash for Django MPTT-based categorization app?

£可爱£侵袭症+ 提交于 2019-12-08 04:34:31
问题 I'm using Django-MPTT to develop a categorization app for my Django project. But I can't seem to get the regex pattern for adding a trailing slash that doesn't also break on child categories. Here's an example URL: http://mydjangoapp.com/categories/parentcat/childcat/ I'd like to be able to use http://mydjangoapp.com/categories/parentcat and have it redirect to the trailing slash version. The same should apply to http://mydjangoapp.com/categories/parentcat/childcat (it should redirect to http