0

Okay, newbie here. So I am working on a project where the requirement is : I have to make a Customer model ,where admin/staff can add the customer's details. Now the problem is admin/staff can add multiple address against a customer.I can't find any proper idea ,how would I make it. But still I made a plan ..

enter image description here

and I added some code.

customer-models.py

from __future__ import unicode_literals
from django.contrib.postgres.fields import JSONField
from decimal import Decimal
from django.db import models
from django.utils.safestring import mark_safe

# Create your models here.
class customer(models.Model):
    first_name = models.CharField(max_length=150)
    last_name = models.CharField(max_length=150)
    profile_image = models.FileField()
    phone_number = models.BigIntegerField()

    def profile(self):
        if self.profile_image!="":
            return mark_safe('<img src="/media/%s" width="150" height="150" />' % (self.profile_image))
        else:
            return "";
    profile.short_description = 'Image'
    # profile.allow_tags = True

Customer_address-models.py

from __future__ import unicode_literals
from customer.models import customer
from django.db import models

# Create your models here.
class Address(models.Model):
    customer = models.ForeignKey(customer,default=0)
    country = models.CharField(max_length=150)
    state = models.CharField(max_length=150)
    city =models.CharField(max_length=150)
    pin_code =  models.BigIntegerField()

    # def customer_address(self):
    #   return fields = ["country","state","city","pin_code"]

customer - admin.py

from __future__ import unicode_literals
from .models import customer
from customer_address.models import Address
from django.contrib import admin

# Register your models here.
class CustomerAddressInline(admin.StackedInline):
    model = Address

class CustomerAdmin(admin.ModelAdmin):
    list_display = ["first_name","last_name","phone_number","profile"]
    list_select_related = True
    inlines = [
        CustomerAddressInline,
    ]

    search_fields = ["first_name","last_name","phone_number"]
    fields = ( "first_name","last_name","phone_number",'profile',"profile_image","customer_address" )
    readonly_fields = ('profile',)
    class Meta:
        model = customer

admin.site.register(customer,CustomerAdmin)

cant figure it out,how I show/edit/instert all fields as address in customer module from admin.A little help would be much appreciated Thank you P.S : I am using django-1.11.10

Prad
  • 150
  • 1
  • 4
  • 15

1 Answers1

0

A couple of things wrong here.

Model names are title cased Customer instead of customer.

Not sure why your using two apps for the customer and customer_address

The image field profile_image should use ImageField as this has validation logic for images.

An will suggest not having methods on the model that return markup for image it should be in the template.

Something like <img src="{{ MEDIA_URL }}{{ image.imgfile.name }}"></img>

Put imports in these groups: future, standard library, third-party libraries, other Django components, local Django component, try/excepts

Take a look at django coding style

Foreign Key with defaults you should ensure the object is created before the migration. Take a look at Setting default value for Foreign Key attribute

from __future__ import unicode_literals

from django.contrib import admin

from .models import Customer, Address

class CustomerAddressInline(admin.StackedInline):
    model = Address

@admin.register(Customer)
class CustomerAdmin(admin.ModelAdmin):
    list_display = ["first_name","last_name","phone_number"]
    inlines = [CustomerAddressInline]

    search_fields = ["first_name","last_name","phone_number"]
    fields = ("first_name","last_name","phone_number", "profile_image",)

Also Clarify your question.

jackotonye
  • 3,537
  • 23
  • 31
  • I am concedering the other issues.. I said before. i have to make multiple address add option from admin view.The address has 4 fields,country,state,city,pin code.. and when admin/staff click on add more address ,a new div would show with all address fields on that customer profile. Please ignore all other issues.Please help me ,how can i manage them,if I planned something wrong.. if not ,how can I do that. – Prad Feb 26 '18 at 03:42