3

I had a model, which I changed to be MPTTModel, which looks like this

class ServiceRequest(EntityBase, MPTTModel):
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)
    field1 = models.ForeignKey(SomeModel1, blank=True, null=True, verbose_name="model1")
    field2 = models.ForeignKey(SomeModel2, blank=True, null=True, verbose_name="model2")

and I have this model as inline in Admin page.

class ServiceRequestsInline(admin.StackedInline):
    model = ServiceRequest
    extra = 0
    fieldsets = (
        (None, {
        'fields': ('parent', 'field1', 'field2',)
        }),
    )
    readonly_fields = ('field2',)

I can create a new model and save it.
Then I'd like to create a new model and set it's parent to previously created model. When I do this and click "Save" i get this

InvalidMove at /admin/app/requests/1/change/

A node may not be made a child of any of its descendants.

Though, I can create the 2-nd model, save it without setting a parent and then edit, set parent to the 1-st and save it successfully.

What do I do wrong?

UPDATE
After using a debugger I detected where exception raises - here.

UPDATE 2 Info about EntityBase

class EntityBase(AuthStampedModel):
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

Where AuthStampedModel is model of django-audit-log package -> here

GriMel
  • 2,272
  • 5
  • 22
  • 40
  • Have you seen this question?http://stackoverflow.com/questions/5795742/django-mptt-and-multiple-parents?rq=1 – norbertoonline Oct 29 '16 at 06:12
  • I have a similar setup in a project and haven't had this problem. The only difference that I can see is the EntityBase model that your ServiceRequest model is inheriting from... Could you provide more details of this model? Alternatively, have you tried reversing the ordering of the inheritance... class ServiceRequest(MPTTModel, EntityBase): – tdsymonds Nov 04 '16 at 11:24
  • @tdsymonds tried reversing the order - no luck. Added info about EntityBase – GriMel Nov 05 '16 at 16:31

0 Answers0