Code

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#14135 closed (invalid)

Admin ignores to_field on ForeignKey

Reported by: vbs Owned by: nobody
Component: Uncategorized Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

i am using django 1.2.1. when i use Foreignkey.to_field , the field in the add step of admin still shows "course object", not the char string i specify to. thus i don't know which course i am adding.

it seems like this ticket: http://code.djangoproject.com/ticket/8648, but i am using django 1.2.1

code:

# models.py
class course(models.Model):
    """
    course model
    """
    num_of_course = models.CharField(max_length = 32, unique = True)
    num_of_student = models.IntegerField()
    send_email_flag = models.IntegerField()
    timestamp = models.DateTimeField(auto_now_add=True)
    

class ticket(models.Model):
    """
    model for student who asks for the notifier
    """
    
    user = models.ForeignKey(User)
    email = models.CharField(max_length = 75)
    course = models.ForeignKey(course, to_field='num_of_course')
    email_num = models.IntegerField()
    stop_email_flag = models.IntegerField()
    timestamp = models.DateTimeField(auto_now_add=True)


#admin.py
class TicketModel(admin.ModelAdmin):
    list_display = ('user', 'email_num', 'course', 'email_num', 'stop_email_flag',
    'timestamp')
    
admin.site.register(models.ticket,TicketModel)

Attachments (0)

Change History (4)

comment:1 follow-up: Changed 4 years ago by mk

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

You must define a unicode method on course if you do not want the admin to show "<course object>". This has nothing to do with to_field on a ForeignKey.

comment:2 Changed 4 years ago by vbs

but if i just want a specified field rather than an object, how can i do?

Thank you.

comment:3 in reply to: ↑ 1 Changed 4 years ago by vbs

Replying to mk:

You must define a unicode method on course if you do not want the admin to show "<course object>". This has nothing to do with to_field on a ForeignKey.

but if i just want a specified field rather than an object, how can i do?

Thank you.

comment:4 Changed 4 years ago by mk

This is an usage question -- please submit it to the django-users mailing list instead.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.