Skip to main content
 首页 » 编程设计

python /Django : Is it wise to use "Item" as a class name in Python (in Django in this case)

2025年05月04日22tuyile006

我正在创建这个:

# models.py 
 
class Item(models.Model): 
    sku = models.CharField(max_length=20) 
 
class Attribute(models.Model): 
    item = models.ForeignKey(Item, related_name='items') 

这会导致 Python 中的命名冲突吗?喜欢:

# views.py 
 
some_object.items.create(sku='123abc') 
 
# Is there a place / way that this could cause errors, like: 
# AttributeError: Bound method 'items' has no attribute "create" 
# Since items() on a dict-like object could be a method to return a list, 
# and Django could add support for .items() on a queryset right? 

如果这是个坏主意,我可以更改名称。

请您参考如下方法:

它看起来确实有点笼统,但也不过是“属性”而已。如果可能的话,我会根据应用程序给它一个前缀。