Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parler with mptt #308

Open
digitalbim opened this issue Jan 20, 2022 · 0 comments
Open

parler with mptt #308

digitalbim opened this issue Jan 20, 2022 · 0 comments

Comments

@digitalbim
Copy link

str returned non-string (type NoneType)
when choosing to enter another language in admin.

Step to reproduce:

In admin you can select one of the language tabs: If you fill in one then save - No problems /Errors. When you then go back into the entry to add another language it causes the no string type error.

Model

class MainMenu(MPTTModel, TranslatableModel):
"""
Model defines the main menu for the application
"""
id = models.UUIDField(
    primary_key=True,
    default=uuid.uuid4,
    editable=False
)

parent = TreeForeignKey(
    "self",
    on_delete=models.CASCADE,
    null=True,
    blank=True,
    related_name='child'
)

translations = TranslatedFields(
    name=models.CharField(
        max_length=50,
        help_text="Enter the display name",
        verbose_name="name",
        blank=True,
        null=True
    ),
    slug=models.SlugField(
        max_length=200,
        db_index=True),



)

icon = models.CharField(
    max_length=40,
    help_text="Enter the icon name",
    verbose_name="Display Icon"
)
url = models.CharField(
    max_length=255,
    help_text="Enter the url name",
    verbose_name="Url"
)
order = models.IntegerField(
    verbose_name="Menu order",
    help_text="Choose menu order",
)

class MPTTMeta:
    order_insertion_by = ['order']

class Meta:
    verbose_name = "Menu item"

def __str__(self):
    return self.safe_translation_getter('name', any_language=True)

objects = NavigationManager()

Manager:

class NavigationQuerySet(TranslatableQuerySet, TreeQuerySet):

def as_manager(cls):
    # make sure creating managers from querysets works.
    manager = NavigationManager.from_queryset(cls)()
    manager._built_with_as_manager = True
    return manager
as_manager.queryset_only = True
as_manager = classmethod(as_manager)

class NavigationManager(TreeManager, TranslatableManager):
     _queryset_class = NavigationQuerySet

Admin:

class NavigationAdminForm(MPTTAdminForm, TranslatableModelForm):
     pass


class NavigationAdmin(TranslatableAdmin, MPTTModelAdmin):
    form = NavigationAdminForm

    def get_prepopulated_fields(self, request, obj=None):
        return {'slug': ('name',)}  # needed for translated fields


admin.site.register(MainMenu, NavigationAdmin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant