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

Creating ManyToMany records in the reverse with excluded fields #1408

Open
dwasyl opened this issue Oct 12, 2024 · 0 comments
Open

Creating ManyToMany records in the reverse with excluded fields #1408

dwasyl opened this issue Oct 12, 2024 · 0 comments

Comments

@dwasyl
Copy link

dwasyl commented Oct 12, 2024

In trying to create ManyToMany relations, ie (with the below example) Topping.objects.first().pizzas.add(pizza) fails, due to the excluded field on Topping.

It generates a HistoricalTopping() got unexpected keyword arguments: 'last_used date', based on simple_history/models.py:L740 when it creates the instance because the excluded field is included in the attrs.

With two models created like this:

class Pizza(models.Model):
    name = models.CharField(max_length=150)
    toppings = models.ManyToManyField("Topping", related_name="pizzas")

    history = HistoricalRecords(m2m_fields=[toppings])

class Topping(models.Model):
    name = models.CharField(max_length=150)
    last_used_date = models.DateTimeField(auto_now=True)

    history = HistoricalRecords(excluded_fields=["last_used_date"])

And then using it like:

topping = Topping.objects.first()
pizza = Pizza.objects.first()

# This fails with an unexpected keyword argument (for the last_used_date)
topping.pizzas.add(pizza)

# But this works with no errors at all
pizza.toppings.add(topping)

This testing was done on the latest release version, running Django 4.2. It's pretty clear that the problem is in the generating of the attrs used to create the history in create_historical_record, but I'm not sure how to prevent it from picking up excluded fields. Any ideas?

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