-
-
Notifications
You must be signed in to change notification settings - Fork 151
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
Error when using "through" intermediate model #187
Comments
Seems this was discussed in this older issue: #77 Unfortunately I'm not proficient enough in Django to give any advice. I have hit the same problem, trying to upgrade an old project that was working fine with a "through" model with
If someone might be able to explain how I could fix this, I would every grateful. Otherwise I fear I may have to rebuild my django app from scratch, because there are so may moving parts to upgrade. |
Have you tried setting an attribute |
@merwok Forgive me, I haven't touched this code (or really, Django at all) in about 7–8 years, and it's currently a foreign country to me. Yes, it looks like if I add the django-sortedm2m/sortedm2m/fields.py Line 291 in 79c434c
Should it be like this in the intermediate model class? class EpisodeGuest(models.Model):
_sort_field_name = "sort_value"
sort_value = models.IntegerField(default=0) UPDATE: I tried this in my local development environment, made migrations, migrated the database, and even after adding a new EpisodeGuest to an episode in the admin, an inspection of the local database shows all To provide more context in my project: # coding=utf-8
from cinefex.models import Entity
from django.db import models
from sortedm2m.fields import SortedManyToManyField
[...]
[...]
class Episode(models.Model):
# An episode of a podcast.
[...]
guests = SortedManyToManyField(
Entity, through="EpisodeGuest", related_name="episode_guests", blank=True
) # Person
def __repr__(self):
return self.designation
def __str__(self):
return self.designation
class EpisodeGuest(models.Model):
# Fixing sort field error for `django-sortedm2m` >= 1.5.0
_sort_field_name = "sort_value"
# extend these through…
created = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
episode = models.ForeignKey(Episode, on_delete=models.CASCADE)
guest = models.ForeignKey(Entity, on_delete=models.CASCADE)
guest_blurb = models.TextField(help_text="Markdown blurb.", blank=True)
guest_links = models.ManyToManyField(ExternalLink, blank=True)
# Special Guests get big headshots in the center column.
is_special = models.BooleanField(
default=False, help_text="Usually only one per episode."
)
def __repr__(self):
return f"{self.episode.designation}: {self.guest.name}"
def __str__(self):
return f"{self.episode.designation}: {self.guest.name}" Thanks for taking a look! |
I think this was asked before but is it possible to define my own intermediate model with sortedm2m using
through
? In my case I have this model:The intermediate table:
I got this error message:
AssertionError: The model is used as an intermediate model by '<class 'myproject.models.SamplePoolIndexCand'>' but has no defined '_sort_field_name' attribute
How can I solve this?
The text was updated successfully, but these errors were encountered: