sprog
sprog

Reputation: 73

Django Admin Adding data

I am trying to learn Django through Coding for Entrepreneur. The problem is in the tutorial, when the tutor adds the models in the admin.py, the values of the fields are shown. While I do it, it shows "something object" only.

models.py

from django.db import models

class Join(models.Model):
    email = models.EmailField() 
    ref_id = models.CharField(max_length=120, default='ABC', unique=True)
    ip_address = models.CharField(max_length=120, default='ABC')
    timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)
    updated = models.DateTimeField(auto_now_add = False, auto_now = True)   

    def __unicode__(self):
        return "%s" %(self.email)

    class Meta:
        unique_together = ("email", "ref_id",)


class JoinFriends(models.Model):
    email = models.OneToOneField(Join, related_name="Sharer")
    friends = models.ManyToManyField(Join, related_name="Friend", null=True, blank=True)
    emailall = models.ForeignKey(Join, related_name="emailall")

    def __unicode__(self):
        print(self.friends.all())
        print(self.emailall)
        print(self.email)
        return (self.friends.all()[0].email)

admin.py

from django.contrib import admin
from .models import Join, JoinFriends 

class JoinAdmin(admin.ModelAdmin):
    list_display = ['__unicode__', 'timestamp', 'updated']

    class Meta:
        model = Join

admin.site.register(Join, JoinAdmin)
admin.site.register(JoinFriends)

How it should be enter image description here How it is happening when I am doing it enter image description here How can I solve this problem.

Thank you for the help

The above shown page should come when clicked on Add join friends. It is shown perfectly in the following page when I add the following code as well in admin.py

class FriendAdmin(admin.ModelAdmin):
    list_display = ['__unicode__']


admin.site.register(JoinFriends, FriendAdmin)

enter image description here However, when I click on Add join friends, the page is as shown above in figure1.

enter image description here

When using def __str___(self) function:

Upvotes: 1

Views: 2918

Answers (3)

G Rice
G Rice

Reputation: 176

As mentioned, Python 3 expects __str__ whereas Python 2 expects __unicode__.

Calling the object in Admin without a __str__ method results in the generic Join object being displayed in admin. Anything you define in the __str__ method will be displayed, instead.

Further, when in Py3, you should not use a call to __unicode__ in your list_display, since it won't know what you're talking about. I'm guessing using the imports you supplied in your own answer allows this to function properly, but moving forward, you should simply use __str__ where you would otherwise use __unicode__.

Upvotes: 0

sprog
sprog

Reputation: 73

This works. When using Python 3, add

from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible
from django.db import models
@python_2_unicode_compatible

and remove list_display from admin.py's class

Appreciated the help. Thanks

Upvotes: 0

Dellkan
Dellkan

Reputation: 1901

I'm guessing that you're using python 3. In python 3, __str__ is used instead of __unicode__.

See https://docs.djangoproject.com/en/1.7/topics/python3/#str-and-unicode-methods for more information.

Upvotes: 1

Related Questions