Answer the question
In order to leave comments, you need to log in
I get the following error when exporting DoesNotExist at /tk_export_csv/ - Street matching query does not exist?
Good day to all, I have a problem with exporting csv file. I'm a beginner, I'm weak with the code, if anyone has a solution, tell me, or tell me where the error is in my code?
#models.py
class Ticket(models.Model):
date = models.DateTimeField(auto_now_add=True, null=True, verbose_name="")
viewticket = models.ForeignKey(ViewTicket, null=True, on_delete=models.CASCADE, verbose_name="Вид заявки", default=True)
master = models.ForeignKey(Master, null=True, on_delete=models.SET_NULL, verbose_name="Мастер", default=True)
regions = models.ForeignKey(Region, null=True, on_delete=models.SET_NULL, verbose_name="Район")
streets = models.ForeignKey(Street, null=True, on_delete=models.SET_NULL, verbose_name="Улица")
status = models.ForeignKey(Status, null=True, on_delete=models.SET_NULL, verbose_name="Статус", default=2)
street_only = models.CharField(blank=True, max_length=100, null=True, verbose_name="Альтернативная улица")
home = models.CharField(max_length = 5, null=True, verbose_name="")
flat = models.CharField(blank=True, max_length = 5, null=True, verbose_name="", default=" ")
phone = models.CharField(max_length = 20, null=True, verbose_name="")
companylogin = models.ForeignKey(Companylogin, null=True, on_delete=models.SET_NULL, verbose_name="", default=True)
logins = models.CharField(max_length = 15, null=True, verbose_name="")
tps_id = models.CharField(blank=True, max_length=100, null=True, verbose_name="", unique=True)
priority = models.ForeignKey(Priority, null=True, on_delete=models.SET_NULL, verbose_name="", default=2)
coment = models.TextField(blank=True, null=True, verbose_name="Комментарий", default=" ")
comment_master = models.TextField(blank=True, null=True, verbose_name="Комментарий", default=" ")
update = models.DateTimeField(auto_now=True)
author = models.ForeignKey('auth.User', null=True, on_delete=models.SET_NULL, verbose_name="Автор", default=True)
#views.py
def ticket_export_csv(request):
response = HttpResponse(content_type='text/csv; charset=windows-1251')
writer = csv.writer(response, delimiter=';', dialect='excel')
writer.writerow(['Дата','Район', 'Улица', 'Альтернативаня улица','Дом', 'Квартира', 'Телефон', 'Оператор','Вид заявки', 'Комментарий'])
response.close()
for ticket in Ticket.objects.filter(status=2).values('date', 'regions','streets', 'street_only', 'home', 'flat', 'phone','author', 'viewticket', 'coment'):
get_viewticket = ViewTicket.objects.get(id=ticket.get('viewticket'))
get_street = Street.objects.get(id=ticket.get('streets'))
get_region = Region.objects.get(id=ticket.get('regions'))
get_author = User.objects.get(id=ticket.get('author'))
ticket['regions'] = get_region.name
ticket['streets'] = get_street.name
ticket['author'] = get_author.first_name
ticket['viewticket'] = get_viewticket.name
writer.writerow([ticket.get('date'), ticket.get('regions'), ticket.get('streets'), ticket.get('street_only'), ticket.get('home'), ticket.get('flat'), ticket.get('phone'), ticket.get('author'), ticket.get('viewticket'), ticket.get('coment')])
response['Content-Disposition'] = 'attachment; filename="ticket.csv"'
return response
Answer the question
In order to leave comments, you need to log in
Most likely, Ticket in streets is empty. And why was values applied when objects (Street and others) were needed further? Either remove it and use ticket.streets.name, keeping in mind that streets can be empty, or write name in values('streets__name') themselves
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question