我想有一个自动完成下拉列表,但问题是当我将其用于“ raste”时,这是“ stocksname”表的外键,它不起作用,但是当将其用于“ user”时,即外键“用户”表,它可以正常工作。
我的“股票名称”模型有什么问题吗?
models.py:
class StocksName(models.Model):
checking= ((_('pending'),_('pending')),
(_('reject'),_('reject')),
(_('approved'),_('approved')),
(_('expired'),_('expired')), )
name=models.CharField(max_length=128,verbose_name=_('stockname'),unique="True")
confirm=models.CharField(choices=checking,max_length=12,verbose_name=_('confirmation'), default=_('pending'))
def __str__(self):
return str(self.name)
class Meta:
verbose_name=_('StocksName')
verbose_name_plural=_('StocksNames')
ordering = ('name',) #For sorting alphabetically
class Stocks(models.Model):
user=models.ForeignKey(User, null=True,related_name='stockdetails')
raste=models.ForeignKey(StocksName, null=True)
stname=models.CharField(max_length=128,blank=True, null=True,verbose_name=_('stockname'))
mark=models.CharField(max_length=128,blank=True, null=True,verbose_name=_('mark'))
pic=models.ImageField(blank=True,null=True,verbose_name=_('pic'),upload_to = 'stocks', default = 'stocks/nopic.jpg')
car=models.ForeignKey(Cars,blank=True,null=True,verbose_name=_('car'),on_delete=models.SET_NULL ,to_field='carname')
description=models.CharField(blank=True,null=True,max_length=264,verbose_name=_('description'))
price=models.PositiveIntegerField(blank=True,null=True,verbose_name=_('price'))
date=models.DateTimeField(auto_now_add = True,verbose_name=_('date'))
checking= ((_('pending'),_('pending')),
(_('reject'),_('reject')),
(_('approved'),_('approved')),
(_('expired'),_('expired')),
)
confirm=models.CharField(choices=checking,max_length=12,verbose_name=_('confirmation'), default=_('pending'))
def __str__(self):
return str(self.id)
class Meta:
verbose_name=_('Stock')
verbose_name_plural=_('Stocks')
def get_absolute_url(self):
return reverse('BallbearingSite:mystocks' )
Forms.py :(用于用户将小部件中的“ raste”替换为“ user”)
class StocksForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(StocksForm, self).__init__(*args, **kwargs)
for field_name, field in self.fields.items():
field.widget.attrs['class'] = 'form-control'
field.widget.attrs['style']= 'width:60%'
class Meta():
model=Stocks
fields=('user','raste','stname','mark','description','pic','price')
widgets = {
'raste': autocomplete.Select2(url='BallbearingSite:stock_autocomplete'),
}
Views.py :(适用于用户,可以正常工作)
class StocksAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
if not self.request.user.is_authenticated():
return User.objects.none()
qs = User.objects.all()
if self.q:
qs = qs.filter(username__istartswith=self.q)
return qs
def has_add_permission(self, request):
return True
Views.py :(对于raste,这不起作用)
class StocksAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
if not self.request.user.is_authenticated():
return StocksName.objects.none()
qs = StocksName.objects.all()
if self.q:
qs = qs.filter(name__istartswith=self.q)
return qs
def has_add_permission(self, request):
return True
urls.py:
url(r'^stock_autocomplete/$',views.StocksAutocomplete.as_view(create_field='name'),name='stock_autocomplete'),