Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django数据库中的引用名称,而不是id_Python_Django_Sqlite_Modelform - Fatal编程技术网

Python Django数据库中的引用名称,而不是id

Python Django数据库中的引用名称,而不是id,python,django,sqlite,modelform,Python,Django,Sqlite,Modelform,我正在发送一封带有模型中对象ID的电子邮件,但我想发送对象名称 我有两种型号: class Uniform(models.Model): category = models.CharField(max_length = 50) description = models.CharField(max_length = 50) price = models.FloatField(max_length = 6) size = models.CharField(max_le

我正在发送一封带有模型中对象ID的电子邮件,但我想发送对象名称

我有两种型号:

class Uniform(models.Model):
    category = models.CharField(max_length = 50)
    description = models.CharField(max_length = 50)
    price = models.FloatField(max_length = 6)
    size = models.CharField(max_length = 10)

    def __str__(self):
        return '{}: {} - {} - ${}'.format(self.category, self.description, self.size, self.price)

class Transaction(models.Model):
    employee_id = models.ForeignKey(Employee, on_delete = models.PROTECT)
    uniform = models.ForeignKey(Uniform, on_delete = models.CASCADE)
    date = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return '{}: {} - {}'.format(self.employee_id, self.uniform, str(self.date))
和forms.py

class TransactionForm(forms.ModelForm):

    class Meta():
        model = Transaction
        fields = '__all__'
我的观点

def employeeCloset(request):
    form = TransactionForm()

    if request.method == 'POST':
        form = TransactionForm(request.POST)

        if form.is_valid():
            subject = 'Checked out item from Me'
            message = 'It is the responsibility of the team member to maintain all articles identified so that they are presentable and worn upon arriving according to standards required in the Employee Handbook. I understand I will be charged the deposit according to the cost identified below: \n {} \n These costs will be deducted from the team member’s paycheck and will be credited upon return of article(s). \n Any item that is not returned by the end of the pay period the cost of the item will be deducted from my paycheck. \n In order to receive full credit from items they must be returned by December 6, 2019.'.format(Uniform.__str__ == form.data['uniform'])
            from_email = settings.EMAIL_HOST_USER
            to_list = ['', settings.EMAIL_HOST_USER]
            form.save(commit = True)

            send_mail(subject, message, from_email, to_list, fail_silently=False)

            return index(request)
        else:
            print('Error form invalid')

    return render(request, 'apparelapp/employeeCloset.html', {'form':form}
在.formatform.data['uniform']处发送电子邮件item views.py时,我在电子邮件中得到的是统一ID号18,而不是整行。我想说的是厨师长外套,没有标志,16.0,4XL不是unirform id:

我试着引用统一的形式,然后是统一的值,没有运气


谢谢Django,新手

您可以使用id查找整个统一对象:

然后您可以参考uniform.description来获取名称

uniform = Uniform.objects.get(id=int(form_data['uniform']))