Display the __str__ or __unicode__ of an objet on deletion of one of its ManyToManyField

70 views
Skip to first unread message

Jean-Baptiste Pressac

unread,
Dec 2, 2016, 8:03:55 AM12/2/16
to Django users
Hello,
On Django 1.8 + Python 2.7.11, I have declared in Django admin.py an oeuvre class with a ManyToManyField (types) to an TypeOeuvre class.


class Oeuvre(models.Model):
    titre = models.CharField(max_length=510)
    types = models.ManyToManyField('TypeOeuvre',
                                   blank=True,
                                   verbose_name="Type(s) de l'œuvre")
    class Meta:
        ordering = ['titre']

    def __unicode__(self):
        return "{0} [{1}]".format(self.titre[:200], self.id)


class TypeOeuvre(models.Model):
    intitule = models.CharField(max_length=100, verbose_name='Intitulé')
    commentaire = models.CharField(max_length=255,
                                   verbose_name='Commentaire',
                                   blank=True)

    class Meta:
        ordering = ['intitule']
        verbose_name = "Type d'œuvre"
        verbose_name_plural = "Types d'œuvres"

    def __unicode__(self):
        return self.intitule


When I try to delete a TypeOeuvre object in Django Admin for which relations to one oeuvre exists, the "Are you sure?" warning do not explicitly mention the name of the oeuvre but displays a list of:

Oeuvre-typeoeuvre relationship: Oeuvre_types object

I also reproduced the problem on Django 1.10 + Python 3.4.5 and using __str__ instead of __unicode__.


Is there a way to display the name of the oeuvres linked to the type ?


Thanks,





Carlos Andre

unread,
Dec 2, 2016, 9:07:42 AM12/2/16
to django...@googlegroups.com
def __unicode__(self):
        return "{0} [{1}]".format(self.titre[:200], self.id).__str__

def __unicode__(self): return self.intitule.__str__


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f62bac64-d292-46ee-b23a-e0fc7a630706%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jean-Baptiste Pressac

unread,
Dec 2, 2016, 9:33:22 AM12/2/16
to django...@googlegroups.com
Hello,
Thanks Carlos but modifying TypeOeuvre as suggested

 def __unicode__(self):
        return self.intitule.__str__

generates the following error : 

Exception Type: TypeError at /admin/prelib/typeoeuvre/11/delete/
Exception Value: __str__ returned non-string (type method-wrapper)



--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/MjQ-Dh2p4eQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.



--
Jean Baptiste Pressac

edu...@niubit.es

unread,
May 10, 2018, 6:45:35 AM5/10/18
to Django users
Hi Jean Baptiste.

Did you find a solution to your issue? I am in the same situation and your explanation is the closest that I have found.

Thanks...
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/MjQ-Dh2p4eQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.



--
Jean Baptiste Pressac
Reply all
Reply to author
Forward
0 new messages