简体   繁体   English

Django如何将模型中的值添加到另一个应用程序的视图中?

[英]Django how to add value from model into another application's view?

I have 2 applications in my "aplikacja" django project: 我的“ aplikacja” django项目中有2个应用程序:

  • articles 用品
  • qr QR

From articles model I would like to get a value "title" from the first article and then put it into qr.views (it prepares for me a pdf file) 从文章模型中,我想从第一篇文章中获取值“ title”,然后将其放入qr.views中(它为我准备了pdf文件)

Articles models: 文章模型:

from django.db import models

class Article(models.Model):
        title = models.CharField(max_length=150)
        content = models.TextField(verbose_name="Zawartosc")
        published = models.DateTimeField(verbose_name="Data Publikacji")

How to get a "title" value into qr views? 如何获得一个“标题”值进入二维视图? I suppose I need to import article from aplikacja.articles.models. 我想我需要从aplikacja.articles.models导入文章。 But how to get exactly value in test_qr method? 但是如何在test_qr方法中获得确切的值?

from reportlab.pdfgen import canvas
from django.http import HttpResponse
from reportlab.graphics.shapes import Drawing 
from reportlab.graphics.barcode.qr import QrCodeWidget 
from reportlab.graphics import renderPDF
from django.contrib.auth.models import User
from aplikacja.articles.models import article


def test_qr(request):
    # Create the HttpResponse object with the appropriate PDF headers.
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'

    a= map(lambda x: str(x), User.objects.values_list('id', flat=True).order_by('id'))

    p = canvas.Canvas(response)
    p.drawString(10, 800, a[1])

    qrw = QrCodeWidget(a[1]) 
    b = qrw.getBounds()

    w=b[2]-b[0] 
    h=b[3]-b[1] 

    d = Drawing(200,200,transform=[200./w,0,0,200./h,0,0]) 
    d.add(qrw)

    renderPDF.draw(d, p, 1, 1)

    p.showPage()
    p.save()
    return response

To get the first article use the first() method: 要获取第一篇文章,请使用first()方法:

article = article.objects.all().order_by('published').first()
title = article.title if article else None

BTW python lists are zero based so to get the first element of a list you should use a[0] instead of a[1] . BTW python列表基于零,因此要获取列表的第一个元素,应使用a[0]而不是a[1] But anyway I suggest you to use the same first() method to get the id of the first user: 但是无论如何,我建议您使用相同的first()方法来获取第一个用户的id

first_user_id = str(User.objects.values_list('id', flat=True) \
                                .order_by('id').first())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在django模型的object创建过程中,如何从另一个模型的object的JSONField中存储的JSON数组中弹出并保存一个值? - During a django model's object creation, how to pop out and save a value from a JSON array stored in JSONField of another model's object? Django如何从另一个模型获取模型中的值 - Django how to get in model the value from another model Django如何从视图中的另一个模型获取字段? - Django How do you get field from another model in a view? 如何在django中使用外部应用程序中的模型添加多对一关系 - How to add many to one relationship with model from external application in django Django查看/模板如何知道模型的最大值 - Django View / Template how to know the max value from a model 如何从另一个模型返回值。 Django 休息框架 - How to return a value from another model. Django Rest Framework Django:如何从另一个 model 更新字段值? - Django:How to update field value from another model? Django:将新外键添加到现有模型中,并将默认值作为同一模型中的另一个外键 - Django: Add new foreign key to existing model with default value as another foreign key from the same model 如何在Django中以多对一关系将一个模型对象添加到另一模型的ModelForm模板中? - How do I add one model objects to another model’s ModelForm template with many to one relationship in Django? Django:从 model 获取 id 以将其添加到另一个 - Django: get the id from on model to add it to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM