a month ago

What method should I use to connect the CustomUser model to my Dog model so that the owner of a newly posted dog is the current user who is logged in?

The goal is to create a new dog object and add it to the database, with the current user as the owner. Despite having no errors, the new dog object is not visible on the admin page. This project comprises two apps: the "petapp" which contains the main logic, and the "users" app which is used to create Custom Users. The code includes an HTML form where users can input dog object details like age, name, date, county, color, description, breed, gender, and status. The post request is handled by a view function in the file which is the one that saves the new dog object to the database. A CustomUser model in the "users" app has a profile image, date of birth, and dogs fields as attributes. Any suggestion that can resolve the issue will be greatly appreciated.
a month ago
Verified Answer
It seems that the new dog object is not showing up on the admin page. This could be due to several reasons. One reason could be that the model has not been registered in the file. You can check if the model has been registered by looking at the file in your "petapp" app and ensuring that the model is included in the list of models registered with the admin site. Another reason could be that you have not added the new fields to your UserAdmin class in You can add your fields to UserAdmin class by unpacking the UserAdmin.fieldsets tuple to a list in your, then edit to insert your field/s in the relevant section and repack as a tuple. I hope this helps. Let me know if you have any other questions.


python - model not showing up in django admin - Stack OverflowCustom User model fields (AbstractUser) not showing in django adminNew fields not showing in django admin page : r/djangolearning - Reddit
a month ago
It looks like you are trying to create a new dog instance and assign the currently logged in user as its owner. However, you are not passing any owner_id in the post request to the view. So, in your file, when you try to get the owner_id using owner_id=request.POST.get('owner_id'), it returns None and later on user=get_object_or_404(CustomUser, id=owner_id) raises a 404 error. To fix this, you can add the owner_id to the dogFormData in the AddDog.vue component using dogFormData.append('owner_id', this.$store.state.userId) (assuming you have the userId stored in a state in your store). Then, in your file, you can get the owner_id from the request using owner_id=request.POST.get('owner_id') and then get the user using user=get_object_or_404(CustomUser, id=owner_id). Also, in your file, you are returning a JsonResponse with dog.to_dict() as its data. However, the owner key in the dictionary returned by to_dict() is already set to the owner's id (or None if there is no owner). So, you don't need to set it again to Instead, you can simply return JsonResponse(dog.to_dict()). Here's how your updated code in the AddDog.vue component and file would look like:
// AddDog.vue component
let new_entry={
  name_d: new_name_d,
  age_d: new_age_d,
  date_d: new_date_d,
  county_d: new_county_d,
  color_d: new_color_d,
  description_d: new_description_d,
  breed_d: new_breed_d,
  gender_d: new_gender_d,
  status_d: new_status_d,
  owner_id: this.$store.state.userId // add owner_id to FormData

dogFormData.append('age_d', new_age_d);
dogFormData.append('name_d', new_name_d);
dogFormData.append('county_d', new_county_d);
dogFormData.append('picture_d', new_picture_d);
dogFormData.append('color_d', new_color_d);
dogFormData.append('description_d', new_description_d);
dogFormData.append('date_d', new_date_d);
dogFormData.append('breed_d', new_breed_d);
dogFormData.append('gender_d', new_gender_d);
dogFormData.append('status_d', new_status_d);
dogFormData.append('owner_id', this.$store.state.userId); // append owner_id to FormData
console.log("this is form data")
let response=await fetch('',{
  method: 'POST',
  body: dogFormData,

def newdog(request):
    if request.method=='GET':
        return JsonResponse({
            'dogs': [
                for dog in Dog.objects.all()
    elif request.method=='POST':
        print('in post')
        owner_id=request.POST.get('owner_id') # get owner_id from request
        user=get_object_or_404(CustomUser, id=owner_id) # get user using owner_id
        return JsonResponse(dog.to_dict()) # return JsonResponse with dog.to_dict() as its data