How to type a m2m_changer receiver function #2259
Alexerson
started this conversation in
Common issues and solutions
Replies: 2 comments 2 replies
-
|
Yes, there is a way to use a type for the through model. You need to declare and (since it's declared you might as well just) use the through model. from django.db import models
class Topping(models.Model):
pass
+class PizzaTopping(models.Model):
+ topping = models.ForeignKey(Topping, on_delete=models.CASCADE)
+ pizza = models.ForeignKey("app.Pizza", on_delete=models.CASCADE)
+
+ class Meta:
+ constraints = [models.UniqueConstraint(fields=["topping", "pizza"], name="topping_pizza_unq")]
+
+
class Pizza(models.Model):
- toppings = models.ManyToManyField(Topping, blank=True)
+ toppings = models.ManyToManyField(Topping, blank=True, through=PizzaTopping) |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
And regarding
Not really, since overloading is for the caller side. But the caller in this case is Django internals. So unless you write code that calls the receiver function an overload won't help you |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I’m trying to type my receiver for a
m2m_changedsignal.So far I have this:
models.pyapps.pyIs there any way to do better for the type hint of
sender?If I’m setting
"type[Pizza.toppings.through]", I’m getting this error:error: Name "Pizza.toppings.through" is not defined [name-defined]Do we have an available generic for this?
reveal_typegives me"type[core.models.Pizza_toppings]".Also, is there any way to use
overloadto improve this?Like
But then, in the function, it obviously doesn’t work:
will give
Maybe simply using
typing.castthen?Anyway, mostly me ranting, but I’m curious if anyone have ideas about that.
Beta Was this translation helpful? Give feedback.
All reactions