我有一个模型,可以保存从excel文件导入的数据。 我想防止有重复的条目,它会检查已存在的数据,看它们是否与那些数据匹配。
我的模特
from django.db import models
class UserData(models.Model):
GENDER_CHOICES = (
('Male', 'Male'),
('Female', 'Female'),
)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
age = models.IntegerField()
gender = models.CharField(default='Male', choices=GENDER_CHOICES, max_length=6)
address = models.CharField(max_length=200)
class Meta:
verbose_name_plural = 'User Data'
def __str__(self):
return self.fullname()
views.py
class UploadFileForm(forms.Form):
file = forms.FileField()
def import_data(request):
if request.method == "POST":
form = UploadFileForm(request.POST,
request.FILES)
if form.is_valid():
request.FILES['file'].save_to_database(
name_columns_by_row=2,
model=UserData,
mapdict=['first_name', 'last_name', 'age', 'gender', 'address'])
return HttpResponse("OK")
else:
return HttpResponseBadRequest()
else:
form = UploadFileForm()
return render_to_response('excel/upload_form.html',
{'form': form},
context_instance=RequestContext(request))
我尝试使用unique_together
并尝试覆盖表单中的cleaned_data
,但仍然无法防止将重复项添加到数据库中。
实现这一目标的最佳方法是什么?感谢
答案 0 :(得分:2)
1.创建一个模型表单
class UserDataForm(forms.ModelForm):
// code here
class Meta:
model = UserData
2。使用。
将Excel工作表数据制作成字典格式from pandas import *
xls = ExcelFile('path_to_file.xls')
df = xls.parse(xls.sheet_names[0])
参考:Python Creating Dictionary from excel data。
3.使用字典数据验证模型表单并检查验证。如果数据有效,则检查相同的db条目或使用django model_name.get_or_create()
form = UploadFileForm(request.POST,
request.FILES)
if form.is_valid():
# code here to convert to dictionary
data = data_in_dict_form
user_data_form = UserDataForm(data=data)
if user_data_form.is_valid():
obj, created = UserData.objects.get_or_create(**user_data_form.cleaned_data)
# this will create new entry if doesnt exist and just get from db if already exist.
# You can ause any method to create an entry to the data base. you can also just .filter().exists() to check if there is an existing entry. if It returns True you dont need to save it.
# Your Response
else:
# your Response
注意:如果有多行,则每行都用于db中的单独条目。你需要循环每一行并验证并保存()/ Do_not_save。 :)
我希望这可以帮助您解决问题。