NameError:未定义全局名称'parentid_id'

时间:2013-09-30 17:21:35

标签: python django django-views

我收到以下错误消息。我无法理解它为什么会发生以及如何解决它。

Internal Server Error: /location/0/
Traceback (most recent call last):
  File "D:\Python27\lib\site-packages\django\core\handlers\base.py", line 115, i
n get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "D:\Project\myproject\radio\views.py", line 33, in location_show
    arrs = Regions.objects.filter(parentid_id in ListFirst)

Modles.py

class Regions(models.Model):
    id =models.AutoField(primary_key=True)
    name = models.CharField(max_length=30)
    english_name = models.CharField(max_length=20)
    parentid = models.ForeignKey('self', blank=True,null=True)
    slug_name = models.SlugField(max_length=40,blank=True)
    regions_level = models.PositiveIntegerField()   
    comment = models.CharField(max_length=50,blank=True,null=True)

class Radio(models.Model):
    station_name = models.CharField(max_length=30)
    description = models.TextField()
    regions = models.ForeignKey(Regions,blank=True,null=True)
    custom_url = models.SlugField(max_length=100,blank=True)
    stream = models.ForeignKey(Stream,related_name='radio_stream')
    attributes = models.OneToOneField(Attributes,blank=True,null=True)
    type = models.ForeignKey(Type,related_name='radio_type')
    tags = models.ManyToManyField(Tag, blank=True)
    comment = models.ForeignKey(Comment,related_name='radio_comment',blank=True,null=True)
    publish_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now_add=True)

Views.py

def location_show(request, id):
    try:
        radio_single = Regions.objects.get(id = id)
        if radio_single.regions_level == 0:
            ars = Regions.objects.filter(parentid_id=id) 
            ListFirst = [f.id for f in ars]
            arrs = Regions.objects.filter(parentid_id in ListFirst) 
            ListSencond = [s.id for s in arrs]
            arrss = Regions.objects.filter(parentid in ListSecond) 
            ListThird = [t.id for t in arrss]
            listFirst.extend(ListSecond)
            ListFirst.extend(listThird)
            ListFirst.append(id)
            radios = Radio.objects.filter(regions in ListFirst)
            return render_to_response("radio_location_show.html",{"ars":ars,"radios":radios})
        elif radio_single.regions_level == 1:
            ars = Regions.objects.filter(parentid_id=id)
            listFirst = [p.id for p in ars]
            arss = Regions.objects.filter(parentid in ListFirst)
            ListsSecond = [s.id for s in arss]
            ListFirst.extend(ListSecond)
            ListFirst.append(id)
            radios = Radio.objects.filter(regions in ListFirst) 
            return render_to_response("radio_location_show.html",{"ars":ars,"radios":radios})
        elif radio_single.regions_level == 2:
            ars = Regions.objects.filter(parentid_id=id)
            ListFirst= [p.id for p in ars]
            ListFirst.append(id)
            radios = Radio.objects.filter(regions in ListFirst)     
            return render_to_response("radio_location_show.html",{"ars":ars,"radios":radios})
        else:
            ars = Regions.objects.filter(parentid_id = id)
            ListFirst = [f.id for f in ars]
            radios = Radio.objects.filter(regions in ListFirst)
            return render_to_response("radio_location_show.html",{"ars":ars,"radios":radios})
        except Radio.DoesNotExist:
            raise Http404
如果有人知道我哪里出错并且可以解释,

真的会感激一点帮助。

1 个答案:

答案 0 :(得分:2)

这不是Django语法(在代码中多次使用):

arrs = Regions.objects.filter(parentid_id in ListFirst) 

你可能意味着

arrs = Regions.objects.filter(parentid__id__in=ListFirst) 

您可能还想更改一行:

ars = Regions.objects.filter(parentid_id=id) 

有双下划线:

ars = Regions.objects.filter(parentid__id=id)