0

I’m trying to create an instance of Listing so I can have user populate in the admin.

I’m new to Django and thought I had it but looks like I’m wrong somewhere.

How do I create an instance of Listing to populate in admin?

Any help i gladly appreciated, thanks.

Code Below:

user_profile/models

from django.db import models
from django.urls import reverse
from django.contrib.auth.models import AbstractUser, UserManager
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from users.forms import CustomUserCreationForm, CustomUserChangeForm
from users.models import CustomUser

class Listing (models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, null=True)
    created =  models.DateTimeField(auto_now_add=True, null=True)
    updated = models.DateTimeField(auto_now=True)
    name = models.CharField(max_length=100)
    address = models.CharField(max_length=100)
    zip_code = models.CharField(max_length=100)
    mobile_number = models.CharField(max_length=100)
    cc_number = models.CharField(max_length=100)
    cc_expiration = models.CharField(max_length=100)
    cc_cvv = models.CharField(max_length=100)    

def create_profile(sender, **kwargs):
    if kwargs['created']:
        user_profile = Listing.objects.create(user=kwargs['instance'])
        
post_save.connect(create_profile, sender=User)

user_profile/admin.py

from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin


from user_profile.forms import HomeForm
from users.forms import CustomUserCreationForm, CustomUserChangeForm

from user_profile.models import Listing
from users.models import CustomUser


# Register models here.

class UserProfileAdmin(admin.ModelAdmin):
    list_display = ['name', 'address', 'zip_code', 'mobile_number', 'created', 'updated', 'user']
    list_filter = ['name', 'zip_code', 'created', 'updated', 'user']

admin.site.register(Listing, UserProfileAdmin)
Community
  • 1
  • 1
spidey677
  • 317
  • 1
  • 10
  • 27

1 Answers1

0

I suspect the problem is that you have a custom user model, but your signal is listening to the post_save event from the built-in User. Since you never create instances of that model, the signal never gets triggered.

Change it to:

post_save.connect(create_profile, sender=CustomUser)
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks for the quick reply my friend. Still no luck. Any other clues? – spidey677 Jan 02 '19 at 23:50
  • I'm getting closer to solving this... It's gladly appreciated If you have any free time I made a new ticket: https://stackoverflow.com/questions/54015285/django-logged-in-user-not-populating-in-admin-py – spidey677 Jan 03 '19 at 05:59