这里我需要从另一个表中选择用户的名字和第二个名字。模型是StockTransfer但我需要来自用户模型
class Meta:
model = StockTransfer
fields = (
'id', 'from_stock', 'to_stock',
'comment', 'products',
'status', 'user',
'transfer_request',
'created_date',
'date_delivery',
'modified_date',
'accepted_time',
'cancelled_time',
'first__name',
'second__name'
)
模型( users.py )
class User(AbstractBaseUser, PermissionsMixin):
first_name = models.CharField(max_length=255)
second_name = models.CharField(max_length=255)
----- stocktransfer.py(model)
class StockTransfer(BaseModel, DeleteMixin):
PENDING = '0'
APPROVED = '1'
CANCELLED = '2'
STATUS_CHOICES = (
(PENDING, 'pending'),
(APPROVED, 'approved'),
(CANCELLED, 'cancelled'),
)
from_stock = models.ForeignKey('main.Stock', related_name='from_transfers')
to_stock = models.ForeignKey('main.Stock', related_name='to_transfers')
comment = models.CharField(max_length=255, blank=True, null=True)
status = models.CharField(max_length=1, choices=STATUS_CHOICES, default=PENDING)
user = models.ForeignKey('main.User', null=True)
我想我可以选择第一个名字和第二个名称,只需在metaclass字段中编写first_name和second_name即可。这样对吗? 有什么想法吗?
答案 0 :(得分:0)
假设stock_transfer是StockTransfer的一个实例:
stock_transfer = StockTransfer.objects.all().get(id=1)
first_name = stock_transfer.user.first_name
second_name = stock_transfer.user.second_name