Re: Django csv export from two objects
I have the this errore: Exception Type: AttributeError Exception Value: 'list' object has no attribute 'name' My the views.py: def export_excel(request): response = HttpResponse(mimetype='application/vnd.ms-excel; charset="Shift_JIS"') response['Content-Disposition'] = 'attachment; filename=file.csv' writer = csv.writer(response) writer.writerow(["No","name""date","time of attendance", "time of leavework"]), att_list = attendance.objects.all() lea_list = leavework.objects.all() all_list = list(chain(att_list, lea_list)) for obj in all_list: row = [] for field in attendance._meta.fields, leavework._meta.fields: row.append(unicode(getattr(obj, field.name).encode("cp932"))) writer.writerow(row) return response 2014年5月19日月曜日 11時41分45秒 UTC+9 hito koto: > > Hello, > > I want user, contact_date, contact_time of attendance and leavework > objects > > so, i use mata.fields of models > > my the models: > > class Staff(models.Model): > user = models.OneToOneField(User, null=False) > user_name = models.CharField(max_length=255) > first_kana = models.CharField(max_length=255) > last_kana = models.CharField(max_length=255) > > def __unicode__(self): > return self.user_name > > class attendance(models.Model): > user = models.ForeignKey(Staff, ) > contact_date = models.DateField( auto_now_add=True) > contact_time = models.TimeField( auto_now_add=True) > > class Meta: > ordering = ['-contact_time'] > def __unicode__(self): > return unicode(self.user) > > class leavework(models.Model): > user = models.ForeignKey(Staff) > contact_date = models.DateField( default=datetime.now) > contact_time = models.TimeField(default=datetime.now) > > class Meta: > ordering = ["-contact_time"] > > def __unicode__(self): > return unicode(self.user) > > > > > > 2014年5月16日金曜日 21時31分25秒 UTC+9 Daniel Roseman: >> >> On Friday, 16 May 2014 12:34:52 UTC+1, hito koto wrote: >>> >>> Hi, >>> >>> I would like to export the csv file as follows: >>> >>> No name >>> date >>> time of attendance >>> time of leavework >>> 01 >>> hito >>> 2014/5/16 10:31:50 20:30 >>> 01 >>> hito >>> 2014/5/16 10:56:23 20:30 01 >>> hito >>> 2014/5/16 10:19:59 20:30 02 >>> charry >>> 2014/5/16 10:18:40 20:30 02 >>> charry >>> 2014/5/16 10:12:52 20:30 02 >>> charry >>> 2014/5/16 10:11:19 20:30 >>> >> >> I have no idea how that relates to my reply. But if you need it like >> that, why are you doing all that mucking about with `_meta.fields`? You >> know exactly what fields you want, you should simply specify them. >> -- >> DR. >> > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/33e3deea-ec6e-4b8f-ae8d-42fe356667a8%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Django csv export from two objects
Hello, I want user, contact_date, contact_time of attendance and leavework objects so, i use mata.fields of models my the models: class Staff(models.Model): user = models.OneToOneField(User, null=False) user_name = models.CharField(max_length=255) first_kana = models.CharField(max_length=255) last_kana = models.CharField(max_length=255) def __unicode__(self): return self.user_name class attendance(models.Model): user = models.ForeignKey(Staff, ) contact_date = models.DateField( auto_now_add=True) contact_time = models.TimeField( auto_now_add=True) class Meta: ordering = ['-contact_time'] def __unicode__(self): return unicode(self.user) class leavework(models.Model): user = models.ForeignKey(Staff) contact_date = models.DateField( default=datetime.now) contact_time = models.TimeField(default=datetime.now) class Meta: ordering = ["-contact_time"] def __unicode__(self): return unicode(self.user) 2014年5月16日金曜日 21時31分25秒 UTC+9 Daniel Roseman: > > On Friday, 16 May 2014 12:34:52 UTC+1, hito koto wrote: >> >> Hi, >> >> I would like to export the csv file as follows: >> >> No name >> date >> time of attendance >> time of leavework >> 01 >> hito >> 2014/5/16 10:31:50 20:30 >> 01 >> hito >> 2014/5/16 10:56:23 20:30 01 >> hito >> 2014/5/16 10:19:59 20:30 02 >> charry >> 2014/5/16 10:18:40 20:30 02 >> charry >> 2014/5/16 10:12:52 20:30 02 >> charry >> 2014/5/16 10:11:19 20:30 >> > > I have no idea how that relates to my reply. But if you need it like that, > why are you doing all that mucking about with `_meta.fields`? You know > exactly what fields you want, you should simply specify them. > -- > DR. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3d5d691b-7f5a-43eb-81ce-296db3c61afb%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Django csv export from two objects
On Friday, 16 May 2014 12:34:52 UTC+1, hito koto wrote: > > Hi, > > I would like to export the csv file as follows: > > No name > date > time of attendance > time of leavework > 01 > hito > 2014/5/16 10:31:50 20:30 > 01 > hito > 2014/5/16 10:56:23 20:30 01 > hito > 2014/5/16 10:19:59 20:30 02 > charry > 2014/5/16 10:18:40 20:30 02 > charry > 2014/5/16 10:12:52 20:30 02 > charry > 2014/5/16 10:11:19 20:30 > I have no idea how that relates to my reply. But if you need it like that, why are you doing all that mucking about with `_meta.fields`? You know exactly what fields you want, you should simply specify them. -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/470ef47a-288d-4333-869b-363c2aa9343e%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Django csv export from two objects
Hi, I would like to export the csv file as follows: No name date time of attendance time of leavework 01 hito 2014/5/16 10:31:50 20:30 01 hito 2014/5/16 10:56:23 20:30 01 hito 2014/5/16 10:19:59 20:30 02 charry 2014/5/16 10:18:40 20:30 02 charry 2014/5/16 10:12:52 20:30 02 charry 2014/5/16 10:11:19 20:30 2014年5月16日金曜日 19時29分59秒 UTC+9 Daniel Roseman: > > > > On Friday, 16 May 2014 10:40:50 UTC+1, hito koto wrote: >> >> Hello, >> >> I want to export csv from the two objects >> I have the erroe is here: >> Exception Type: AttributeError Exception Value: >> >> 'QuerySet' object has no attribute 'id' >> >> >> >> >> This is my the views.py : >> >> def export_excel(request): >> >> response = HttpResponse(mimetype='application/vnd.ms-excel; >> charset="Shift_JIS"') >> response['Content-Disposition'] = 'attachment; filename=file.csv' >> writer = csv.writer(response) >> writer.writerow(["No","name", "date", "time"]), >> >> obj_all=set([attendance.objects.all()]) >> l_all = set([leavework.objects.all()]) >> #all = obj_all | l_all >> A = obj_all >> B = l_all >> for obj in A: >> row = [] >> # all = row >> for field in attendance._meta.fields: >> row.append(unicode(getattr(obj,field.name)).encode("cp932")) >> for leave in B: >> for field in leavework._meta.fields: >> row.append(unicode(getattr(leave, field.name >> )).encode("cp932")) >> for all in row: >> writer.writerow(all) >> >> return response >> > > You're doing some very odd things here. You're putting the queryset into a > list and then putting that list into a set: so that when you iterate > through the set, what you actually get is the queryset as one object, not > the individual objects within it. > > You could fix this by just doing > obj_all=set(attendance.objects.all()) > - ie without the surrounding [] - but it's not clear why you think you > need a set in the first place. A queryset will never give you duplicate > objects, because it's a simple representation of the db query, which can't > itself have duplicates. You might have duplicate *data* within the table, > but that won't be de-duplicated by the set(), as the rows themselves are > still unique. > -- > DR. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d73fb2ec-2c7d-46e5-b136-e9c46e27e97f%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Django csv export from two objects
On Friday, 16 May 2014 10:40:50 UTC+1, hito koto wrote: > > Hello, > > I want to export csv from the two objects > I have the erroe is here: > Exception Type: AttributeError Exception Value: > > 'QuerySet' object has no attribute 'id' > > > > > This is my the views.py : > > def export_excel(request): > > response = HttpResponse(mimetype='application/vnd.ms-excel; > charset="Shift_JIS"') > response['Content-Disposition'] = 'attachment; filename=file.csv' > writer = csv.writer(response) > writer.writerow(["No","name", "date", "time"]), > > obj_all=set([attendance.objects.all()]) > l_all = set([leavework.objects.all()]) > #all = obj_all | l_all > A = obj_all > B = l_all > for obj in A: > row = [] > # all = row > for field in attendance._meta.fields: > row.append(unicode(getattr(obj,field.name)).encode("cp932")) > for leave in B: > for field in leavework._meta.fields: > row.append(unicode(getattr(leave, field.name > )).encode("cp932")) > for all in row: > writer.writerow(all) > > return response > You're doing some very odd things here. You're putting the queryset into a list and then putting that list into a set: so that when you iterate through the set, what you actually get is the queryset as one object, not the individual objects within it. You could fix this by just doing obj_all=set(attendance.objects.all()) - ie without the surrounding [] - but it's not clear why you think you need a set in the first place. A queryset will never give you duplicate objects, because it's a simple representation of the db query, which can't itself have duplicates. You might have duplicate *data* within the table, but that won't be de-duplicated by the set(), as the rows themselves are still unique. -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a6e0b583-a6be-42f8-aabc-cfafca125ef7%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.