如何将外键值插入表单?

时间:2019-03-25 16:31:17

标签: python django

我正在设置一个Web应用程序,该应用程序可以预订从一个目的地到另一个目的地的运输。我的问题是表单显示了所有属性,但是它不允许我插入外键,也不能从列表中选择正确的外键。

import requests
from bs4 import BeautifulSoup as bs
import pandas as pd

baseUrl = 'https://www.flinders.edu.au'

emails = []
positions = []

with requests.Session() as s:
    r = s.get('https://www.flinders.edu.au/directory/index.cfm/search/results?page=1&lastnamesearch=A&firstnamesearch=&ousearch=')
    soup = bs(r.content, 'lxml')
    names, urls = zip(*[ (item.text, baseUrl + item['href']) for item in soup.select('td:first-child a')])
    tels = [item.text for item in soup.select('td:nth-of-type(2) a')]

    for url in urls:
        r = s.get(url)
        soup = bs(r.content, 'lxml')
        positions.append(soup.select_one('.staffInfo + td').text)
        emails.append(soup.select_one('[href^=mailto]').text)

final = list(zip(names, tels, positions, emails))
df = pd.DataFrame(final, columns = ['name', 'tel', 'position', 'email'])
print(df.head())
df.to_csv(r'C:\Users\User\Desktop\data.csv', sep=',', encoding='utf-8-sig',index = False )

forms.py

从Django导入表格 从.models导入Auftrag

CreateOrder(forms.ModelForm)类:

with requests.Session() as s:
    r = s.get('https://www.flinders.edu.au/directory/index.cfm/search/results?page=1&lastnamesearch=A&firstnamesearch=&ousearch=')
    soup = bs(r.content, 'lxml')
    data =  [item.text for item in soup.select('.directory-person')]
    names = data[0::2]
    tels = data[1::2]

、、、、、、、、、、、、、、、、、、、、、、、、、、、

views.py 从django.shortcuts导入渲染 从.models导入顺序 从.forms导入CreateOrder

def creation_order_view(request):     形式= CreateOrder(request.POST或无)

class Loading_location(models.Model):
address = models.CharField(max_length=40)
postal_code = models.CharField(max_length=10)
place = models.CharField(max_length=30)

class Deloading_location(models.Model):
address = models.CharField(max_length=40)
postal_code = models.CharField(max_length=10)
place = models.CharField(max_length=30)

class Customer(models.Model):
company = models.CharField(max_length=40)
address = models.CharField(max_length=40)
postal_code = models.CharField(max_length=10)
place = models.CharField(max_length=30)

class Truck(models.Model):
mark = models.CharField(max_length=20)
brand = models.CharField(max_length=20)
type = models.CharField(max_length=20)

class Order(models.Model):
order_date = models.DateField()
customer = models.ForeignKey(Customer, on_delete=models.PROTECT, null=True, default=None)
loading_location = models.ForeignKey(Loading_location, on_delete=models.PROTECT, null=True, default=None)
deloading_location = models.ForeignKey(Deloading_location, on_delete=models.PROTECT, null=True, default=None)
truck = models.ForeignKey(Truck, on_delete=models.PROTECT, null=True, default=None)
price = models.FloatField(max_length=7)

预期的输出结果是,我可以从列表中选择外键以完成订单并将其保存在数据库中。 预先谢谢你

0 个答案:

没有答案