Django管理模型架构

时间:2015-07-16 18:48:14

标签: django django-models django-admin

django的新手,到目前为止爱它。我有一个django管理员,架构设计问题。我有一个拥有客户,产品和订单表的应用程序。客户有一个产品订单,由admin预先构建,并保存在CustomerProduct表中。当客户登录时,他们只能查看他们的订单,更新数量,以及它。他们无法访问产品表或其他客户订单。我现在卡住的三个部分,

  1. 订单表应与customer_products表具有多对多关系。
  2. 订单历史记录需要以某种方式保存在单独的表中,因此如果用户的订单更改,我们仍会记录其过去的订单。我需要为用户构建一个只显示其订单的视图。 /管理/ onlineordering / ordersheet。
  3. 如何设置此网址并限制对经过身份验证的用户的访问权限。用户A只能看到用户A的CustomerProduct(订单)
  4. 以下是apps models.py和admin.py

    onlineordering / models.py

    from django.conf import settings
    from django.db import models
    from datetime import datetime
    
    class Customer(models.Model):
        customer = models.ForeignKey(settings.AUTH_USER_MODEL, limit_choices_to={'groups__name': "customers"})
        customer_product = models.ManyToManyField('Product', through='CustomerProduct')
        company_name = models.CharField(max_length=255)
        address1 = models.CharField(max_length=255)
        address2 = models.CharField(max_length=255)
        city = models.CharField(max_length=255)
        state = models.CharField(max_length=255)
        zip_code = models.CharField(max_length=255)
        def __unicode__(self):
            return self.company_name
    
    class CustomerProduct(models.Model):
        customer = models.ForeignKey('Customer')
        product = models.ForeignKey('Product')
    
    class Product(models.Model):
        item = models.CharField(max_length=255)
        description = models.CharField(max_length=255)
        def __unicode__(self):
            return self.description
    
    class Order(models.Model):
        customer_product_order = models.ManyToManyField('CustomerProduct', through='CustomerProductOrder')
        purchase_order_number = models.CharField(max_length=10)
        person_placing_order = models.CharField(max_length=255)
        requested_delivery_date = models.DateField(blank=True, null=True)
    
    class CustomerProductOrder(models.Model):
        order = models.ForeignKey('Order')
        customer_product = models.ForeignKey('CustomerProduct')
        quantity = models.IntegerField(default=0)
    

    onlineordering / admin.py

    from django.contrib import admin
    from django.contrib.auth.admin import UserAdmin
    
    from .models import Order,Customer,Product
    
    UserAdmin.add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'password1', 'password2', 'first_name', 'last_name', 'groups')}
        ),
    )
    
    class CustomerProductInline(admin.StackedInline):
        model = Customer.customer_product.through
        extra = 0
    
    class ProductAdmin(admin.ModelAdmin):
        inlines = [
            CustomerProductInline,
        ]
    
    class CustomerAdmin(admin.ModelAdmin):
        inlines = [
            CustomerProductInline,
        ]
        exclude = ('customer_product',)
    
    admin.site.register(Order)
    admin.site.register(Customer,CustomerAdmin)
    admin.site.register(Product)
    

    更新

    /onlineordering/views.py

    from django.http import HttpResponse
    
    
    def ordersheet(request):
        return HttpResponse("Hello, world.")
    

    /urls.py

    from django.conf.urls import include, url
    from django.contrib import admin
    
        urlpatterns = [
            url(r'^admin/', include(admin.site.urls)),
            url(r'^admin/onlineordering/ordersheet', include(admin.site.urls)),
        ]
    

0 个答案:

没有答案