如何从django中的模型中获取一个字段

时间:2021-09-09 19:36:03

I have such a model, in my django application. I want to draw only one field of this model and put them in the view. My solution below does not work:

我的django应用程序中有这样的模型。我想只绘制该模型的一个字段并将它们放在视图中。我的解决方案不起作用:

obj = Text.objects.get(subsID)

My model

我的模特

result = braintree.Subscription.create({
        "payment_method_token": payment_method_token,
        "plan_id": "67mm"
        })

subscription_id = result.subscription.id

class Text(models.Model):
    title = models.CharField(max_length=255)
    text = models.TextField()
    date_from = models.DateTimeField('date from', blank=True, null=True)
    date_to = models.DateTimeField('date to', blank=True, null=True)
    subsID = models.CharField(default=subscription_id, max_length=255)

    def __unicode__(self):
        return self.title

My view

我的看法

def get_history(request):
    subscription_id = Text.objects.filter(subsID)
    history = braintree.Subscription.find(subscription_id)
    return render(request, "sheet/history.html", {"history": history})

2 个解决方案

#1


10  

Generally, When filter or get, you have to put query inside it, like

通常,当filter或get时,你必须在其中放入查询,比如

subscription_id = Text.objects.filter(fieldname="searchterm")

This will return a queryset.So to view this

这将返回一个queryset.So来查看它

subscription_id.values() #returns a list of objects(dicts)

If you want to get only subsID

如果你想只获得subsID

subscription_id.values("subsID")

This also return you list which contains

这也返回包含的列表

[{"subsID":"value"}, {"subsID":"value"} ....]

If you want to get only values

如果你想只获得价值

subscription_id.values_list("subsID", flat=True)

This will return like

这将返回

["value", "value", ....]

#2


1  

You have to equal subsID to the value you want to find.

您必须将subsID与您要查找的值相等。

subscription_id = Text.objects.filter(subsID=<your subscrition id variable>)

pay attention this will return a list []

注意这会返回一个列表[]

subscription_id = Text.objects.get(subsID=<your subscrition id variable>)

This will return an object

这将返回一个对象

#1


10  

Generally, When filter or get, you have to put query inside it, like

通常,当filter或get时,你必须在其中放入查询,比如

subscription_id = Text.objects.filter(fieldname="searchterm")

This will return a queryset.So to view this

这将返回一个queryset.So来查看它

subscription_id.values() #returns a list of objects(dicts)

If you want to get only subsID

如果你想只获得subsID

subscription_id.values("subsID")

This also return you list which contains

这也返回包含的列表

[{"subsID":"value"}, {"subsID":"value"} ....]

If you want to get only values

如果你想只获得价值

subscription_id.values_list("subsID", flat=True)

This will return like

这将返回

["value", "value", ....]

#2


1  

You have to equal subsID to the value you want to find.

您必须将subsID与您要查找的值相等。

subscription_id = Text.objects.filter(subsID=<your subscrition id variable>)

pay attention this will return a list []

注意这会返回一个列表[]

subscription_id = Text.objects.get(subsID=<your subscrition id variable>)

This will return an object

这将返回一个对象