Skip to content

Widget/field not present when custom through is used #218

@petrklus

Description

@petrklus

I need to use a custom through model with the SortedManyToManyField. However, as soon as I add the field settings, the field widget is no longer present in the admin.

My code:

class TestFeature(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name



class TestProductDetail(models.Model):

    name = models.CharField(max_length=100)
    features = SortedManyToManyField(
        TestFeature,
        sort_value_field_name="sort_number",
        related_name="product_details",
        through="ProductDetailFeatureThrough",
        through_fields=("testproductdetail", "testfeature"),
    )

    def __str__(self):
        return self.name


class ProductDetailFeatureThrough(models.Model):
    _sort_field_name = "sort_number"

    testfeature = models.ForeignKey(TestFeature, on_delete=models.CASCADE)
    testproductdetail = models.ForeignKey(TestProductDetail, on_delete=models.CASCADE)
    param_one = models.CharField(max_length=50, blank=True, default="")
    param_two = models.CharField(max_length=50, blank=True, default="")
    sort_number = models.IntegerField(default=0)

    def __str__(self):
        return "Relationship to {0} extra params <{1}> <{2}>".format(
            self.testfeature.name, self.param_one, self.param_two
        )

My admin is equally simple:

@admin.register(TestProductDetail)
class TestProductDetailAdmin(admin.ModelAdmin):
    pass


@admin.register(TestFeature)
class TestFeatureAdmin(admin.ModelAdmin):
    pass

When I comment out the through and through_fields, I get the widget to appear again in the admin and it works fine (but of course does not use my custom through model).

No errors are thrown in either scenario, just the field is not present.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions