如何针对ID为ID的对象创建对象 [英] How can I create an object against an object with id

查看:78
本文介绍了如何针对ID为ID的对象创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想针对一个对象创建一个对象.例如.我想创建一个票证",只有在创建了线索"后才能创建票证.因此,他们之间存在一对多的关系.一个线索可以有很多票.尽管票证是针对潜在客户创建的,但我无法通过模板对其进行管理.

so I would like to create an object against an object. for example. I want to create a 'ticket' which can only be made once there is a 'lead' created. Therefore they have one to many relationship. One lead can have many tickets. Although the tickets are being created against the lead but I cant manage it to do via template.

下面是代码.

models.py

models.py

class Lead(models.Model):
    lead_title  = models.CharField(max_length=255, null=True, blank=True)
    agent_id    = models.IntegerField(null=True, blank=True)
    email       = models.EmailField(null=True, blank=True)
    .......


class Ticket(models.Model):
    lead   = models.ForeignKey(Lead, on_delete=models.CASCADE, blank=True, null=True)
    passenger_name = models.CharField(max_length=255, null=True, blank=True)
    .......

views.py

def detail_lead(request, id): 
    lead = Lead.objects.get(id=id)
    ticket = lead.ticket_set.all()

    context = {

        'lead' : lead,
        'ticket' : ticket,

    }

    return render(request, 'lead/detail_lead.html', context)

def create_ticket(request, id):
    ticket = Ticket.objects.get(id=id)
    if request.method == 'POST':
        form = Ticket_form(request.POST)
        if form.is_valid():
            form.save()
            print(form)
            return redirect('lead:listlead')
    else:
        form = Ticket_form()

    context = {
        'form' : form
    }
    return render(request, 'ticket/create_ticket.html', context)

urls.py


path('detaillead/<int:id>', detail_lead, name="detaillead"),
.....
path('createticket/<int:id>/', create_ticket, name="createticket"),

detail_lead.html

detail_lead.html

<a href="{% url 'createticket' lead.id %}"><button type="button" class="btn btn-success">Add
                      Ticket</button></a>

因此detail_lead.html具有用于添加票证"的按钮,该按钮转到添加票证页面,但是一旦创建票证,它就不会针对该特定当前潜在客户创建票证.当我在管理页面中看到并查找创建的票证时,它没有选择任何潜在顾客".

So the detail_lead.html has a button for "Add Ticket" which goes to the add ticket page but once I create the ticket it does not create the ticket against this particular current lead. and when I see in the admin page and look up for the created ticket it has not selected any "lead".

我在这里做错了什么.我只想针对当前线索创建故障单.我知道我必须将销售线索的ID传递到"a"标签中,但仍然没有发生.我在这里做什么错了?

What am I doing wrong here. I want to create a ticket only against this current lead. I know I have to pass the id of a lead into an "a" tag but its still not happening. What am I doing wrong here?

比你提前

推荐答案

您从此处发送的ID"{%url'createticket'lead.id%}"是销售线索ID,但是为什么要使用它来查找一张票?仍然要创建票证时,通过ID查找票证的目的是什么?

The id you are sending from here "{% url 'createticket' lead.id %}" is the lead id, but why are you using that to find a ticket? What is the purpose of finding a ticket by id when you are going to create one anyway?

使用该ID查找销售线索,并在创建票证之后,在票证和销售线索之间建立关系.

Use that id to find the Lead and once the ticket has been created, make the relationship between the ticket and the lead.

应该是

lead = Lead.objects.get(id=id)
....
..... 
if form.is_valid():
    item = form.instance
    item.save()
    lead.ticket_set.add(item)
    lead.save()

参考:
https://docs.djangoproject.com/zh-CN/3.0/topics/db/examples/many_to_one/#many-to-one-relationships

这篇关于如何针对ID为ID的对象创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆