Open AliIslamov opened 10 months ago
Hey! 👋 No need to override the TopicCreateView
view to perform what you are trying to do. Unfortunately what you are trying to do (ie. change the implementation of the save()
method of the Topic
model) may not be possible in an elegant way: the default save()
implementation still comes from the AbstractTopic
class, and since your method is setting the slug
and then calling super().save()
, then the default implementation essentially overrides your slug value.
If you absolutely want this to work you could do something like this:
class Topic(AbstractTopic):
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
self.slug = slugify(unidecode(self.subject))
self._simple_save()
Where you essentially call the default save()
implementation, then set your intended slug
value and then do another save. But it means two saves performed at the DB level.
The best way forward would be to make it easier to customize how the slug is generated in the library (eg. by extracting the slug assignment into a dedicated method in AbstractTopic
so that it can be easily replaced).
I'm trying to override Topic.save method for forum_conversation model.
My goal is correct transliteration of Cyrillic into slug.
For this I did the following:
1) Created apps folder in root.
2) Created
and
3) Create apps/forum_conversation/admin.py :
4) Create apps/forum_conversation/model.py :
5) Commented line:
And have wrote in settings.py :
As a result, while creating forum topic slug still consists of Cyrillic alphabet. Its look like model customisations are not picked up.
In Oscar docs I see:
In your overriding models.py, ensure that you import Oscar’s models after your custom ones have been defined. If that doesn’t help, you have an import from oscar.apps.*.models somewhere that is being executed before your models are parsed. One trick for finding that import: put assert False in the relevant Oscar’s models.py, and the stack trace will show you the importing module.
So I wrote line assert False in original forum_conversation/abstract_models.py :
And see while creating new topic this traceback:
So, I conclude that it is also necessary to override class TopicCreateView for forum_conversation/views.py :
But I can't understand what exactly do I need to change? 🤔
It is interesting that if I commended the whole method def save in original forum_conversation/abstract_models.py then everything works well and I get correct transliteration in topic slug.