0

I have a contact us class in django model,that using "user_id" foreign key,in admin.py file,i want using userprofile data with user_id key,such as "name" filed,in userprofile model "user_id" is foreign_key too,how can do it?

in models.py :

class Ticket(models.Model):
    user_id = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, editable=False, verbose_name="user")
    replier_id = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, related_name="replier")
    guest_name = models.CharField(max_length=50, blank=True, null=True, editable=False)
    guest_email = models.CharField(max_length=255, blank=True, null=True, editable=False)
    user_phone = models.CharField(max_length=13, blank=True, null=True, editable=False)
    subject = models.CharField(max_length=255, blank=True, null=True, editable=False)
    message = models.TextField(editable=False)
    reply = models.TextField(blank=True, null=True)
    date = models.DateTimeField(auto_now=True, editable=False)
    reply_date = models.DateTimeField(blank=True, null=True)
    user_ip = models.GenericIPAddressField(editable=False)

in admin.py

class ContactUsAdmin(admin.ModelAdmin):
    actions = None
    fields = ('user_name', 'user_email', 'subject', 'date', 'message', 'user_phone', 'reply')
    list_display = ('user_name', 'user_email', 'subject', 'date', 'is_replied')
    list_filter = ('date',)
    search_fields = ['user_id__first_name', 'user_id__email', 'guest_name', 'guest_email', 'subject', 'message']
    readonly_fields = ('user_name', 'user_email', 'subject', 'date', 'message', 'user_phone', 'reply_date')

i want show name from UserProfile class in admin.py fields

Jahongir Rahmonov
  • 13,083
  • 10
  • 47
  • 91
Farzaneh Torkpichlou
  • 2,306
  • 3
  • 14
  • 18

2 Answers2

0
By the question title of your query. I am giving an incentive how to do that in django:
    class Album(models.Model):
        AlbumName = models.CharField(max_length=250)
        AlbumSinger = models.CharField(max_length=250)
        AlbumGenre = models.CharField(max_length=250)
        AlbumImage = models.FileField(null=True)
        SingerImage = models.FileField(null=True)
    class Musics(models.Model):
        album = models.ForeignKey(Album, related_name='music')
        MusicName = models.CharField(max_length=250)
        MusicLength = models.CharField(max_length=250)
        MusicFile = models.FileField(null=True)

Using foreignkey album in Musics. you can access to all the music according to their album. And to access them in admin, just import class name and do the following:

    admin.site.register(Album) 
    admin.site.register(Musics)

I hope it will clear your confusion.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
  • if singer details saved in another model or table,how do you get singer fields with album foreign key from musics admin file? – Farzaneh Torkpichlou Oct 17 '17 at 08:43
  • @farzaneh use Musics foreign key in singer model. This will get every information regarding an album or musics or singer. or you could album foreign key in singer model like I did in musics. Thanks – Mahedi Hasan Jisan Oct 17 '17 at 08:56
  • https://stackoverflow.com/questions/4443190/djangos-manytomany-relationship-with-additional-fields – Mahedi Hasan Jisan Oct 17 '17 at 10:31
  • in your example,if you have a singer table that has a user_id foreign key,if you have address of singer,that it is in userprofile table,with "user_id.profile.address" you can get it from userprofile table,that user_id in it has "profile" related name! – Farzaneh Torkpichlou Oct 21 '17 at 06:18
  • Well, It would be easier if you use Album in Singer model as foreign key. This will get you album, musics ans singer all at once! – Mahedi Hasan Jisan Oct 22 '17 at 06:36
-4

finally i resolve it,when in userprofile model,user_id has "profile" related_name,in every where,with user_id foreign key we have all related field from every model that user_id is foreign key in it you can see those with using dir("you user_id instance"). so,with user_id.profile.name i get the profile name of user,without any importing or change!

Farzaneh Torkpichlou
  • 2,306
  • 3
  • 14
  • 18
  • 3
    Please provide a minimal working example. Otherwise your answer is pretty useless. Also please go over your ortographie both in the question and the answer. – Christian Benke Dec 07 '18 at 09:13