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

Deleting temporary elements in Many To Many list causes graphical and functional bug #264

Open
Riccardo-Maffei opened this issue Sep 16, 2024 · 1 comment

Comments

@Riccardo-Maffei
Copy link

Maybe a duplicate of #212 , but I think there still are some differences, therefore I created this new issue. In order to reproduce the bug, a models.ManyToManyField with a through property has to be created. The Model referenced in the "through" should have an Inline class, setting all its fields to autocomplete mode.

Then the user has to create a few empty M2M fields in the admin site,
image

delete one or more in the middle of the list
image

and add a new one.
image

At this point the newly created element can still be deleted, but the autocomplete function won't work on this field anymore, making the input of new data in this field impossible.

Up until now I've found two manual fixes, either adding one field at a time or removing empty fields only if they are at the end of the list. These strategies could be a bit annoying however, especially for people that need to enter information quickly.

I will include pieces of my code in the comments, in case this isn't a bug and it was me who made a mistake.

@Riccardo-Maffei
Copy link
Author

Riccardo-Maffei commented Sep 16, 2024

container_models.py :

from django.db import models
from django.forms import ModelForm

from .models.item_models import Item


class Container(models.Model):
    random_text = models.CharField(
        max_length=200,
        blank=False,
        null=False,
    )

    connections = models.ManyToManyField(
        Item,
        through='ContainerToItemConnector',
    )


class ContainerForm(ModelForm):
    class Meta:
        model = Container
        fields = '__all__'

item_models.py :

from django.db import models
from django.contrib import admin


class Item(models.Model):
    name = models.CharField(
        max_length=200,
        blank=False,
        null=False,
    )


class ItemAdmin(admin.ModelAdmin):
    search_fields = ['name']

container_to_item_connector_models.py :

import nested_admin

from django.db import models
from django.contrib import admin
from django.forms import ModelForm

from .models.item_models import Item
from .models.container_models import Container, ContainerForm


class Property(models.Model):
    class Meta:
        verbose_name_plural = 'Properties'

    name = models.CharField(
        max_length=200,
        blank=False,
        null=False,
    )


class PropertyAdmin(admin.ModelAdmin):
    search_fields = ['name']


class ContainerToItemConnector(models.Model):
    container = models.ForeignKey(
        Container,
        on_delete=models.CASCADE,
    )

    item = models.ForeignKey(
        Item,
        on_delete=models.CASCADE,
    )

    connections = models.ManyToManyField(
        Property,
    )


class ContainerToItemConnectorForm(ModelForm):
    class Meta:
        model = ContainerToItemConnector
        fields = '__all__'


class ContainerToItemConnectorAdmin(admin.ModelAdmin):
    form = ContainerToItemConnectorForm
    autocomplete_fields = ['item', 'connections']


class ContainerToItemConnectorInline(nested_admin.NestedTabularInline):
    model=ContainerToItemConnector
    form=ContainerToItemConnectorForm
    autocomplete_fields = ['item', 'connections']
    extra = 0


class ContainerAdmin(nested_admin.NestedModelAdmin):
    inlines=[ContainerToItemConnectorInline]
    form=ContainerForm

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