English 中文(简体)
a. 显示外地构成外国钥匙,进入模板
原标题:Display a field form a foreign key onto a template
class Product(models.Model):
    
    model=models.CharField(max_length=50, null=True)
    serial=models.CharField(max_length=50, null=True)
    hd_size=models.CharField(max_length=50,null=True)
    ram=models.CharField(max_length=50, null=True)
    processor=models.CharField(max_length=50, null=True)
    product_type = models.CharField(max_length=10, null=True)
    date_purchased = models.DateField(null=True)
    date_created = models.DateTimeField(default=timezone.now)
    date_updated = models.DateTimeField(auto_now=True)
    employee = models.ForeignKey(Employee, on_delete=models.SET_NULL, null=True)

我试图从这一模型中显示数据,但外国关键一栏“雇员”在模板上没有显示。

在此,我的模板

<div class="container-fluid">
    <form method=  POST  id="product-form">
        {% csrf_token %}
        <input type="hidden" name="id" value="{{ product.id }}">
        <div class="form-group mb-3 ">
            <label for="model" class="control-label">Model</label>
            <input type="text" class="form-control rounded-0" id="model" name="model" value="{{ product.model}}" required>
                </div>
        <div class="form-group mb-3 ">
            <label for="serial" class="control-label">Serial No</label>
            <input type="text" class="form-control rounded-0" id="serial" name="serial" value="{{ product.serial}}" required>
        </div>
        <div class="form-group mb-3 ">
            <label for="hd_size" class="control-label">HD Size</label>
            <input type="text" class="form-control rounded-0" id="hd_size" name="hd_size" value="{{ product.hd_size}}" required>
        </div>
        <div class="form-group mb-3 ">
            <label for="ram" class="control-label">RAM</label>
            <input type="text" class="form-control rounded-0" id="ram" name="ram" value="{{ product.ram}}" required>
        </div>
        <div class="form-group mb-3 ">
            <label for="processor" class="control-label">Processor</label>
            <input type="text" class="form-control rounded-0" id="processor" name="processor" value="{{ product.processor}}" required>
        </div>
        <div class="form-group mb-3 ">
            <label for="product_type" class="control-label">Type</label>
            <input type="text" class="form-control rounded-0" id="product_type" name="product_type" value="{{ product.product_type}}" required>
        </div>
            <div class="form-group mb-3 ">
                <label for="date_purchased" class="control-label">date purchased</label>
                <input type="date" class="form-control rounded-0" id="date_purchased" name="date_purchased" value="{{ product.date_purchased}}" required>
            </div>
            <div class="form-group">
                <label for="employee">Employee:</label>
                <select class="form-control" id="employee" name="employee">
                  {% for employee in form.employee.field.queryset %}
                  <option value="{{ employee.address }}">{{ employee.address }}</option>
                  {% endfor %}
                </select>
              </div>     
  
    </form>
</div>

外国钥匙的田间本应允许我从雇员表格中选择雇员电子邮件地址清单后退。

我在这里的看法。 y

# product
@login_required
def product_mgt(request):
    context[ page_title ] = "Computer List"
    products = Product.objects.all()
    context[ products ] = products

    return render(request,  product_mgt.html , context)

@login_required
def save_product(request):
    resp = { status : failed , msg :  }
    if request.method ==  POST :
        if (request.POST[ id ]).isnumeric():
            product = Product.objects.get(pk=request.POST[ id ])
        else:
            product = None
        if product is None:
            form = SaveProduct(request.POST)
        else:
            form = SaveProduct(request.POST, instance= product)
        if form.is_valid():
            form.save()
            
            messages.success(request,  Computer has been saved successfully. )
            resp[ status ] =  success 
            
        else:
            for fields in form:
                for error in fields.errors:
                    resp[ msg ] += str(error + "<br>")
    else:
        resp[ msg ] =  No data has been sent. 
    return HttpResponse(json.dumps(resp), content_type =  application/json )
     
问题回答

谢谢。 我已设法解决这一问题,创建了一个单独的表格,以输入外国关键领域,然后利用这一看法加以挽救。

def assign_employee_to_product(request, product_id):
    product = get_object_or_404(Product, pk=product_id)
    if request.method == "POST":
        form = AssignEmployeeForm(request.POST, instance=product)
        if form.is_valid():
            form.save()
            messages.success(request,  Employee has been assigned a computer successfully. )
            
            
    else:
        form = AssignEmployeeForm(instance=product)
    return render(request,  assign.html , { form : form,  product : product})




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签