1

I've read a lot of questions with answers including here and here but without an answer working. Here are my 2 models that are problematic:

# coding=UTF-8

from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _

from app.models.generic import BaseModel
from app.models.personne import Personne


@python_2_unicode_compatible
class Message(BaseModel):
    src = models.ForeignKey('Personne', related_name='message_src')
    dst = models.ForeignKey('Personne', related_name='message_dst')
    is_read = models.BooleanField(default=False)
    message = models.TextField(null=True, blank=True,
                               verbose_name=_(u'Messages'))
    def __str__(self):
        return u'{} : {} <> {} ({}) : "{}"'.format(
            self.date_creation.strftime('%Y-%m-%d %H:%M:%S'),
            self.src.full_name(), self.dst.full_name(),
            _(u'read') if self.is_read else _(u'unread'),
            self.message_summary()
        ).strip()

    class Meta:
        ordering = ["date_creation"]


@python_2_unicode_compatible
class Conversation(BaseModel):

    personnes = models.ManyToManyField(Personne, related_name='conversations')
    messages = models.ManyToManyField(Message, related_name='conversations')

    order_with_respect_to = 'messages'

    def __str__(self):
        return _(u'Conversation n.{}').format(self.pk).strip()

Each time I do a migrate I keep getting this question, and I always answer yes:

The following content types are stale and need to be deleted:

    app | conversation_personnes
    app | conversation_messages

Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.

    Type 'yes' to continue, or 'no' to cancel:  yes

Process finished with exit code 0

This is not a possible duplicate of this question.

Here's the snapshot of my django_content_type table, there are no model like conversation_personnes or conversation_messages:

django content type table

Thus if I try:

>>> from django.contrib.contenttypes.models import ContentType
ct = ContentType.objects.get(app_label='app', model='conversation_personnes')
ct.delete()

Traceback (most recent call last):
  File "<input>", line 2, in <module>
  File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 334, in get
    self.model._meta.object_name
DoesNotExist: ContentType matching query does not exist.

The only places in my whole code where I find conversation_personnes is a migration file that contains:

File 0060_messagethrough.py

class Migration(migrations.Migration):

    dependencies = [
        ('app', '0059_auto_20160226_1752'),
    ]

    operations = [
        migrations.CreateModel(
            name='MessageThrough',
            fields=[
            ],
            options={
                'proxy': True,
            },
            bases=('app.conversation_messages',),
        ),
    ]

...and

File 0058_auto_20160225_0106.py

class Migration(migrations.Migration):

    dependencies = [
        ('app', '0057_conversation_personnes'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='message',
            name='conversation',
        ),
        migrations.AddField(
            model_name='conversation',
            name='messages',
            field=models.ManyToManyField(to='app.Message'),
        ),
    ]

What am I missing?

Community
  • 1
  • 1
Olivier Pons
  • 15,363
  • 26
  • 117
  • 213

2 Answers2

0

The problem comes from my admin.py where I create a proxy class: those 2 proxies are here so I'm able to change the label in the admin panel. So here's the final code that works = answer to my question, but I have to remove my proxy class, so I can't change the label in the admin... if anyone has a working idea I'm your man!

# class MessageThrough(Conversation.messages.through):
#     class Meta:
#         proxy = True
#
#     def __unicode__(self):
#         return str(self.message)


class ConversationMessagesInline(CollapsedStackedInline):
    # model = MessageThrough
    model = Conversation.messages.through
    fields = ('message',)
    raw_id_fields = ('message',)
    extra = 0

    verbose_name = u"Message"
    verbose_name_plural = u"Messages"


# class PersonneThrough(Conversation.personnes.through):
#     class Meta:
#         proxy = True
#
#     def __unicode__(self):
#         return self.personne.full_name()


class ConversationPersonnesInline(CollapsedStackedInline):
    # model = PersonneThrough
    model = Conversation.personnes.through
    fields = ('personne',)
    raw_id_fields = ('personne',)
    extra = 0

    verbose_name = u"Personne"
    verbose_name_plural = u"Personnes"
Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
0

its a bit late to answer, but I just found this, for the people running to the same problem.

I found this, and the solution number 1, worked for me:

class MyInline(admin.TabularInline):
    MyModel.m2m.through.__unicode__ = lambda x: 'My New Str'
    model = MyModel.m2m.through

Remember to change 'unicode' to 'str' if you are using Python 3+.

Community
  • 1
  • 1
arrt_
  • 369
  • 1
  • 6
  • 15