我有一个模型“Listing”代表公司,还有另一个模型“lead”代表可以分配给公司的不同项目。它们通过多对多关系关联,因为公司可以分配给许多潜在客户,并且许多潜在客户可以与许多其他公司相关联。型号如下。
潜在客户在列表管理页面中显示为内联元素。还有一个片段用于显示直接指向通过反向 url 完成的对象的编辑页面的链接。
问题是 object.id 参数总是错误的,不对应于潜在客户 ID,而是一些其他数字。
在上图中,潜在客户查找 ID 显示为“53”。但实际上它是 78。任何指向我可能缺少的内容的指针将不胜感激。
#admin.py
def url_to_edit_object(self,object):
#info = (object._meta.app_label, object._meta.module_name)
admin_url = reverse('admin:dirapp_lead_change', args=(object.id,))
return u'<a href="%s">Edit</a>' %(admin_url)
主要型号:
class lead(models.Model):
slug = models.SlugField(unique=True,blank=True, null=True)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100, blank=True, null=True)
business_name = models.CharField(max_length=100,blank=True, null=True)
email = models.EmailField(max_length=75, blank=True, null=True)
phone_number = models.CharField(max_length=20, blank=True, null=True)
address = models.CharField(max_length=150, blank=True, null=True)
city = models.CharField(max_length=50, blank=True, null=True)
state = models.CharField(max_length=10, blank=True, null=True)
zipcode = models.CharField(max_length=5, blank=True, null=True)
submission_date = models.DateTimeField(auto_now_add=True, blank=True)
assigned_to = models.ManyToManyField(Listing,blank=True, null=True, related_name="leads")
requested_software = models.CharField(max_length=50, blank=True, null=True)
number_of_pos = models.CharField(max_length=2, choices=NO_OF_POS, default='1')
type_of_business = models.CharField(max_length=30, choices=TYPE_OF_BUSINESS, default='Bar NightClub', blank=True, null=True)
time_frame = models.CharField(max_length=10, choices=TIME_FRAME, default='1')
comments = models.TextField(blank=True, null=True)
def __unicode__(self):
return self.business_name
@models.permalink
def get_absolute_url(self):
return ('listing_detail', (),
{
'slug' :self.slug,
})
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.business_name)
super(lead, self).save(*args, **kwargs)
上市模式:
class Listing(models.Model):
"""
Base model for listings
"""
STATUS_CHOICES = (
('A', 'Active'),
('P', 'Pending'),
)
status = models.CharField(max_length=1, choices=STATUS_CHOICES, default='P')
slug = models.SlugField(unique=True,blank=True, null=True)
submitted_by = models.ManyToManyField(User, blank=True, null=True, related_name="associates")
title = models.CharField(max_length=120, blank=True, null=True)
point = models.PointField(geography=True,srid=settings.SRID, blank=True, null=True)
point2 = models.CharField(max_length=120, blank=True, null=True)
address = models.CharField(max_length=120, blank=True, null=True)
city = models.CharField(max_length=60, blank=True, null=True)
state = models.CharField(max_length=60, blank=True, null=True)
zipcode = models.CharField(max_length=5, blank=True, null=True)
industry = models.ManyToManyField(indCat, related_name="category", blank=True, null=True)
description = models.TextField(blank=True, null=True)
objects = models.GeoManager()
tags = TaggableManager(blank=True)
search = SphinxSearch()
def __unicode__(self):
#return "%s %s %s"%(self.title, self.point.x, self.point.y)
return "%s"%(self.title)
@models.permalink
def get_absolute_url(self):
return ('listing_detail', (),
{
'slug' :self.slug,
})
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
if not self.point:
location = "%s+%s+%s+%s"%(self.address, self.city, self.state, self.zipcode)
cord = get_lat_long(location)
#x,y = cord.split(",")
lat = float(cord[0])
lng = float(cord[1])
#x = float(lat)
#y = float(lng)
self.point = Point(lng,lat)
self.point2 = Point(lng,lat)
super(Listing, self).save(*args, **kwargs)
迪拉普管理员.py:
class ListingImageInline(admin.StackedInline):
model = ListingImage
class AssignedLeads(admin.TabularInline):
model = lead.assigned_to.through
extra = 0
#list_select_related = True
def url_to_edit_object(self,object):
#info = (object._meta.app_label, object._meta.module_name)
admin_url = reverse('admin:dirapp_lead_change', args=(object.id,))
return u'<a href="%s">Edit</a>' %(admin_url)
url_to_edit_object.allow_tags = True
list_display = ('business_name','url_to_edit_object',)
readonly_fields = ('url_to_edit_object',)
class leadAdmin(admin.ModelAdmin):
list_editable = ('city', 'state')
list_display = ('business_name','first_name','last_name','city','state','submission_date')
list_filter = ('city', 'state','submission_date', 'assigned_to')
inlines = [leadUpdateInline]
filter_horizontal = ('assigned_to',)
class ListingAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug":("title",)}
inlines = [leadUpdateInline,ListingImageInline, AssignedLeads]
filter_horizontal = ('submitted_by',)
class Media:
js = (settings.STATIC_URL+'tinymce/jscripts/tiny_mce/tiny_mce.js',
settings.STATIC_URL+"filebrowser/js/TinyMCEAdmin.js",
settings.STATIC_URL+"tinymce/jscripts/tiny_mce/tinymce_setup.js")
class EmailReminderAdmin(admin.ModelAdmin):
list_filter = ('lead', 'company','email_date_time')
admin.site.register(lead,leadAdmin)
admin.site.register(Listing,ListingAdmin)
admin.site.register(indCat)
admin.site.register(email_reminder, EmailReminderAdmin)