nonetype'对象没有属性'startswith'

时间:2019-05-06 05:43:00

标签: django

我有以下型号:

class Shipment(TenantModel):                                                                                                                             
    id = models.BigIntegerField(primary_key=True)                               
    adjusted_ordered_at = models.DateTimeField()                                
    created_at = models.DateTimeField()                                         
    shipped_at = models.DateTimeField(null=True)                            
    store = models.ForeignKey(Store, on_delete=models.CASCADE)                  
    tenant_id = 'store_id'                                                      

    class Meta:                                                                 
        ordering = ['created_at', 'status']                                     
        unique_together = ['id', 'store']                                       

    def __str__(self):                                                          
        return "%s, %s" % (self.id, self.status) 






class Fulfillment(TenantModel):                                                 
    id = models.BigIntegerField(primary_key=True)
    shipment = TenantForeignKey('Shipment', on_delete=models.CASCADE)
    product_instance = TenantForeignKey(ProductInstance,                        
                                        on_delete=models.CASCADE) 


class ProductInstance(TenantModel):                                             
    id = models.BigIntegerField(primary_key=True)                                                
    product = TenantForeignKey(Product, on_delete=models.CASCADE)


class Product(TenantModel):                                                     
    id = models.BigIntegerField(primary_key=True)                                                                    
    product_type = models.CharField(max_length=32) 

我要过滤所有产品类型不等于“订阅”的货件。

由此获得正确的结果:

  Shipment.objects.filter(Q(fulfillment__product_instance__product__product_type='subscription'))

获取AttributeError: 'NoneType' object has no attribute 'startswith'

Shipment.objects.filter(~Q(fulfillment__product_instance__product__product_type='subscription'))

这是堆栈跟踪

AttributeError                            Traceback (most recent call last)
/usr/local/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

/usr/local/lib/python3.6/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    398                         if cls is not object \
    399                                 and callable(cls.__dict__.get('__repr__')):
--> 400                             return _repr_pprint(obj, self, cycle)
    401 
    402             return _default_pprint(obj, self, cycle)

/usr/local/lib/python3.6/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    693     """A pprint that just redirects to the normal repr function."""
    694     # Find newlines and replace them with p.break_()
--> 695     output = repr(obj)
    696     for idx,output_line in enumerate(output.splitlines()):
    697         if idx:

/usr/local/lib/python3.6/site-packages/django/db/models/query.py in __repr__(self)
    242 
    243     def __repr__(self):
--> 244         data = list(self[:REPR_OUTPUT_SIZE + 1])
    245         if len(data) > REPR_OUTPUT_SIZE:
    246             data[-1] = "...(remaining elements truncated)..."

/usr/local/lib/python3.6/site-packages/django_multitenant/models.py in __iter__(self)
     54     def __iter__(self):
     55         # self.add_tenant_filters_with_joins()
---> 56         return super(TenantQuerySet,self).__iter__()
     57 
     58     def aggregate(self, *args, **kwargs):

/usr/local/lib/python3.6/site-packages/django/db/models/query.py in __iter__(self)
    266                - Responsible for turning the rows into model objects.
    267         """
--> 268         self._fetch_all()
    269         return iter(self._result_cache)
    270 

/usr/local/lib/python3.6/site-packages/django/db/models/query.py in _fetch_all(self)
   1181     def _fetch_all(self):
   1182         if self._result_cache is None:
-> 1183             self._result_cache = list(self._iterable_class(self))
   1184         if self._prefetch_related_lookups and not self._prefetch_done:
   1185             self._prefetch_related_objects()

/usr/local/lib/python3.6/site-packages/django/db/models/query.py in __iter__(self)
     52         # Execute the query. This will also fill compiler.select, klass_info,
     53         # and annotations.
---> 54         results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
     55         select, klass_info, annotation_col_map = (compiler.select, compiler.klass_info,
     56                                                   compiler.annotation_col_map)

/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py in execute_sql(self, result_type, chunked_fetch, chunk_size)
   1046         result_type = result_type or NO_RESULTS
   1047         try:
-> 1048             sql, params = self.as_sql()
   1049             if not sql:
   1050                 raise EmptyResultSet

/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py in as_sql(self, with_limits, with_col_aliases)
    458                 # (see docstring of get_from_clause() for details).
    459                 from_, f_params = self.get_from_clause()
--> 460                 where, w_params = self.compile(self.where) if self.where is not None else ("", [])
    461                 having, h_params = self.compile(self.having) if self.having is not None else ("", [])
    462                 result = ['SELECT']

/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py in compile(self, node, select_format)
    388             sql, params = vendor_impl(self, self.connection)
    389         else:
--> 390             sql, params = node.as_sql(self, self.connection)
    391         if select_format is FORCE or (select_format and not self.query.subquery):
    392             return node.output_field.select_format(self, sql, params)

/usr/local/lib/python3.6/site-packages/django/db/models/sql/where.py in as_sql(self, compiler, connection)
     79         for child in self.children:
     80             try:
---> 81                 sql, params = compiler.compile(child)
     82             except EmptyResultSet:
     83                 empty_needed -= 1

/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py in compile(self, node, select_format)
    388             sql, params = vendor_impl(self, self.connection)
    389         else:
--> 390             sql, params = node.as_sql(self, self.connection)
    391         if select_format is FORCE or (select_format and not self.query.subquery):
    392             return node.output_field.select_format(self, sql, params)

/usr/local/lib/python3.6/site-packages/django/db/models/sql/where.py in as_sql(self, compiler, connection)
     79         for child in self.children:
     80             try:
---> 81                 sql, params = compiler.compile(child)
     82             except EmptyResultSet:
     83                 empty_needed -= 1

/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py in compile(self, node, select_format)
    388             sql, params = vendor_impl(self, self.connection)
    389         else:
--> 390             sql, params = node.as_sql(self, self.connection)
    391         if select_format is FORCE or (select_format and not self.query.subquery):
    392             return node.output_field.select_format(self, sql, params)

/usr/local/lib/python3.6/site-packages/django/db/models/lookups.py in as_sql(self, compiler, connection)
    353         if self.rhs_is_direct_value() and max_in_list_size and len(self.rhs) > max_in_list_size:
    354             return self.split_parameter_list_as_sql(compiler, connection)
--> 355         return super().as_sql(compiler, connection)
    356 
    357     def split_parameter_list_as_sql(self, compiler, connection):

/usr/local/lib/python3.6/site-packages/django/db/models/lookups.py in as_sql(self, compiler, connection)
    161     def as_sql(self, compiler, connection):
    162         lhs_sql, params = self.process_lhs(compiler, connection)
--> 163         rhs_sql, rhs_params = self.process_rhs(compiler, connection)
    164         params.extend(rhs_params)
    165         rhs_sql = self.get_rhs_op(connection, rhs_sql)

/usr/local/lib/python3.6/site-packages/django/db/models/lookups.py in process_rhs(self, compiler, connection)
    344                 self.rhs.clear_select_clause()
    345                 self.rhs.add_fields(['pk'])
--> 346             return super().process_rhs(compiler, connection)
    347 
    348     def get_rhs_op(self, connection, rhs):

/usr/local/lib/python3.6/site-packages/django/db/models/lookups.py in process_rhs(self, compiler, connection)
    218             return self.batch_process_rhs(compiler, connection)
    219         else:
--> 220             return super().process_rhs(compiler, connection)
    221 
    222     def resolve_expression_parameter(self, compiler, connection, sql, param):

/usr/local/lib/python3.6/site-packages/django/db/models/lookups.py in process_rhs(self, compiler, connection)
     90             value = value.resolve_expression(compiler.query)
     91         if hasattr(value, 'as_sql'):
---> 92             sql, params = compiler.compile(value)
     93             return '(' + sql + ')', params
     94         else:

/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py in compile(self, node, select_format)
    388             sql, params = vendor_impl(self, self.connection)
    389         else:
--> 390             sql, params = node.as_sql(self, self.connection)
    391         if select_format is FORCE or (select_format and not self.query.subquery):
    392             return node.output_field.select_format(self, sql, params)

/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py in as_sql(self, compiler, connection)
   1004 
   1005     def as_sql(self, compiler, connection):
-> 1006         return self.get_compiler(connection=connection).as_sql()
   1007 
   1008     def resolve_lookup_value(self, value, can_reuse, allow_joins):

/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py in as_sql(self, with_limits, with_col_aliases)
    458                 # (see docstring of get_from_clause() for details).
    459                 from_, f_params = self.get_from_clause()
--> 460                 where, w_params = self.compile(self.where) if self.where is not None else ("", [])
    461                 having, h_params = self.compile(self.having) if self.having is not None else ("", [])
    462                 result = ['SELECT']

/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py in compile(self, node, select_format)
    388             sql, params = vendor_impl(self, self.connection)
    389         else:
--> 390             sql, params = node.as_sql(self, self.connection)
    391         if select_format is FORCE or (select_format and not self.query.subquery):
    392             return node.output_field.select_format(self, sql, params)

/usr/local/lib/python3.6/site-packages/django/db/models/sql/where.py in as_sql(self, compiler, connection)
     79         for child in self.children:
     80             try:
---> 81                 sql, params = compiler.compile(child)
     82             except EmptyResultSet:
     83                 empty_needed -= 1

/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py in compile(self, node, select_format)
    388             sql, params = vendor_impl(self, self.connection)
    389         else:
--> 390             sql, params = node.as_sql(self, self.connection)
    391         if select_format is FORCE or (select_format and not self.query.subquery):
    392             return node.output_field.select_format(self, sql, params)

/usr/local/lib/python3.6/site-packages/django/db/models/fields/related_lookups.py in as_sql(self, compiler, connection)
    128                     lookup_class(target.get_col(self.lhs.alias, source), val), AND)
    129             return root_constraint.as_sql(compiler, connection)
--> 130         return super().as_sql(compiler, connection)
    131 
    132 

/usr/local/lib/python3.6/site-packages/django/db/models/lookups.py in as_sql(self, compiler, connection)
    161     def as_sql(self, compiler, connection):
    162         lhs_sql, params = self.process_lhs(compiler, connection)
--> 163         rhs_sql, rhs_params = self.process_rhs(compiler, connection)
    164         params.extend(rhs_params)
    165         rhs_sql = self.get_rhs_op(connection, rhs_sql)

/usr/local/lib/python3.6/site-packages/django/db/models/lookups.py in process_rhs(self, compiler, connection)
    258                     'one result using slicing.'
    259                 )
--> 260         return super().process_rhs(compiler, connection)
    261 
    262 

/usr/local/lib/python3.6/site-packages/django/db/models/lookups.py in process_rhs(self, compiler, connection)
     90             value = value.resolve_expression(compiler.query)
     91         if hasattr(value, 'as_sql'):
---> 92             sql, params = compiler.compile(value)
     93             return '(' + sql + ')', params
     94         else:

/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py in compile(self, node, select_format)
    388             sql, params = vendor_impl(self, self.connection)
    389         else:
--> 390             sql, params = node.as_sql(self, self.connection)
    391         if select_format is FORCE or (select_format and not self.query.subquery):
    392             return node.output_field.select_format(self, sql, params)

/usr/local/lib/python3.6/site-packages/django/db/models/expressions.py in as_sql(self, compiler, connection)
    732     def as_sql(self, compiler, connection):
    733         qn = compiler.quote_name_unless_alias
--> 734         return "%s.%s" % (qn(self.alias), qn(self.target.column)), []
    735 
    736     def relabeled_clone(self, relabels):

/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py in quote_name_unless_alias(self, name)
    379             self.quote_cache[name] = name
    380             return name
--> 381         r = self.connection.ops.quote_name(name)
    382         self.quote_cache[name] = r
    383         return r

/usr/local/lib/python3.6/site-packages/django/db/backends/postgresql/operations.py in quote_name(self, name)
     97 
     98     def quote_name(self, name):
---> 99         if name.startswith('"') and name.endswith('"'):
    100             return name  # Quoting once is enough.
    101         return '"%s"' % name

AttributeError: 'NoneType' object has no attribute 'startswith'

0 个答案:

没有答案