Code Monkey home page Code Monkey logo

Comments (5)

vdboor avatar vdboor commented on May 29, 2024

Which order are you using for the inheritance? You can also set the form explicitly. The form needs to inherit the TranslatableModelForm off course.

from django-parler.

vdboor avatar vdboor commented on May 29, 2024

In django-fluent-pages, the following inheritance chain is used:

class UrlNodeAdminForm(MPTTAdminForm, TranslatableModelForm):
    # The form class
    pass

class UrlNodeParentAdmin(TranslatableAdmin, PolymorphicMPTTParentModelAdmin):
    # The polymorphic parent admin.
    base_model = UrlNode  

class UrlNodeChildAdmin(PolymorphicMPTTChildModelAdmin, TranslatableAdmin):
    # The polymorphic child admin.
    base_model = UrlNode
    base_form = UrlNodeAdminForm

Just for reference:

class PolymorphicMPTTParentModelAdmin(PolymorphicParentModelAdmin, MPTTModelAdmin):
    pass

class PolymorphicMPTTChildModelAdmin(PolymorphicChildModelAdmin, MPTTModelAdmin):
    pass

from django-parler.

jrief avatar jrief commented on May 29, 2024

I actually don't need django-polymorphic-tree, so I just use django-polymorphic.
I've been so far already, and for existing objects this works fine. Please have a look at https://github.com/edoburu/django-parler/blob/master/docs/polymorphicmodel.rst for details on how the models are defined.

The problem arises, when I add a new product. First I have to choose between Book or Pen. After I filled the model specific fields for the first time, and then try to save that new object, the form is rejected telling me, that the field "Type" is mandatory and that I have to choose between Book or Pen, showing me the same form as at the beginning.

This is my admin.py for testing the demo:

from django.contrib import admin
from parler.admin import TranslatableAdmin, TranslatableModelForm
from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin
from myapp.models.myproduct import BaseProduct, Book, Pen  # from docs/polymorphicmodel.rst

class BookAdmin(TranslatableAdmin, PolymorphicChildModelAdmin):
    base_form = TranslatableModelForm
    base_model = BaseProduct
    base_fields = ('code', 'price', 'name', 'slug')

class PenAdmin(TranslatableAdmin, PolymorphicChildModelAdmin):
    base_form = TranslatableModelForm
    base_model = BaseProduct
    base_fields = ('code', 'price', 'identifier',)

class BaseProductAdmin(PolymorphicParentModelAdmin):
    base_model = BaseProduct
    child_models = ((Book, BookAdmin), (Pen, PenAdmin),)
    list_display = ('code', 'price',)

admin.site.register(BaseProduct, BaseProductAdmin)

Each model admin (polymorphic, parler) by itself works fine, however combining them causes this issue. BTW, if I switch the order of inheritance, the language tabs disappear.

from django-parler.

vdboor avatar vdboor commented on May 29, 2024

After I filled the model specific fields for the first time, and then try to save that new object, the form is rejected telling me, that the field "Type" is mandatory and that I have to choose between Book or Pen, showing me the same form as at the beginning.

The logic for that happens in PolymorphicParentModelAdmin.add_view(). For your own debugging, please look at that code too. First of all, which Django version are you using? Can you see whether the ct_id GET parameter is passed to the next page?

BTW, if I switch the order of inheritance, the language tabs disappear.

That's caused by which modeladmin defines the change_form_template. You can overwrite it yourself too if you like, and combine both templates there.

from django-parler.

jrief avatar jrief commented on May 29, 2024

Thanks for your hints, I now fixed this bug in django-polymorphic.

One question remains. I pilfered your pattern with the add_preserved_filters. However I did not find any API docs about this in Django. Could you please point me onto some docs, where this function is described in detail.

from django-parler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.