Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Error 'long' has no attribute provision [RSS Feed]

#1 Jan. 19, 2011 17:18:53

J.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Error 'long' has no attribute provision


Background:
In a financial application I need to archive a set of reports at the
end of each reporting period, the model is reasonably complex but I
have 3 models of direct concern, a 'provision', 'period' and
'reportheader'. The code is correctly generating the reports and they
are archived in a subdirectory under the media root and the
'reportheader' information in the MySQL database looks perfectly fine.

Problem is that I can not read the information back from the
database. I have tried to read a single report header specifying the
pk=1 (MySQL Query return the record just fine) and get the error
reported in the subject line raised from the 'get'. Of course what I
really want is to get the related set of reports from the period, but
the code to do that returns an empty querySet.

Model code:
class ReportHeader(models.Model):
"""comment"""
provision = models.ForeignKey(Provision, null=False, blank=False)
period = models.ForeignKey(Period, null=False, blank=False)
report_name = models.CharField(max_length=100, null=False)
# couple of othe field definitions
saved_file = models.FileField(upload_to="reports", null=True,
blank=True)

def __init__(self, request, *args, **kwargs)
self.provision=request.provision
self.period=self.provision.get_current_period()

Report Generation Code:
filename, report = report_generator.report(request)
report_header=ReportHeader(request)
report_header.report_name=report_name
load = InMemoryUploadedFile(file=report,
field_name=report_header.saved_file, name=filename,
content_type="application/pdf", size=report.tell(), charset=None)
report_header.saved_file=load
report_header.save()

Failing Code:
def reports_list(request, period_id)
# some other irrelevant code
try:
reportheader=ReportHeader.objects.get(pk=1
except Exception, e:
pass
return render_to_response("reports_list.html, locals(),
context_instance=RequestContext(request))

Sample database content:
id provision_id period_id
report_name ..... saved_file
1 1 4 Net Income Per
Book reports/NIPBQ12010AllCompanies.pdf


Does anyone have a clue why the simple get is failing?

Thank You.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 19, 2011 17:42:33

Łukasz R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Error 'long' has no attribute provision


On 19 January 2011 18:06, John <jspay...@verizon.net> wrote:
> def reports_list(request, period_id)
>   # some other irrelevant code
>   try:
>      reportheader=ReportHeader.objects.get(pk=1
>   except Exception, e:
>      pass
>   return render_to_response("reports_list.html, locals(),
> context_instance=RequestContext(request))

1) You don't want to modify __init__() of your model like this:

def __init__(self, request, *args, **kwargs)
self.provision=request.provision
self.period=self.provision.get_current_period()

If the new constructor requires an additional "request", how does the
ORM create instances of it ? It doesn't know anything about request.
You can make a helper function. Or if you really want to be a part of
the class, make an alternative constructor:

@classmethod
def from_provision(cls, provision):
instance = cls()
instance.provision = provision
instance.perio = provision.get_current_period()

# in your view code::

header = ReportHeader.from_provision(requrest.provision)

2) The moment you wrote "locals()" all the code in your view becomes
relevant, because you're passing all the local variables to your
template. This seems very popular amongst some groups, but I
personally consider this not only bad style, but error-prone and
possibly dangerous.

3) Catching exceptions like that most likely masks the real problem
(Like the fact, that creating an instance of ReportHeader failed, due
to buggy constructor). After all, you're rendering the template even
if you failed to fetch the data you most likely need to render it.

--
Łukasz Rekucki

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 19, 2011 23:29:12

J.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Error 'long' has no attribute provision


Thank you very much for your response.

I guess I should have realized the problem with the constructor. The
'report_header' was a virtual record that was previously never written
to the database, I inherited the further development of this code and
I am still learning some of the details of python/django.

I did simplify the code slightly, the exception handling is a little
better than the code that I posted in that it returns an error
response, but thank you for the advice.


On Jan 19, 12:38 pm, Łukasz Rekucki <lreku...@gmail.com> wrote:
> On 19 January 2011 18:06, John <jspay...@verizon.net> wrote:
>
> > def reports_list(request, period_id)
> >   # some other irrelevant code
> >   try:
> >      reportheader=ReportHeader.objects.get(pk=1
> >   except Exception, e:
> >      pass
> >   return render_to_response("reports_list.html, locals(),
> > context_instance=RequestContext(request))
>
> 1) You don't want to modify __init__() of your model like this:
>
>   def __init__(self, request, *args, **kwargs)
>      self.provision=request.provision
>      self.period=self.provision.get_current_period()
>
> If the new constructor requires an additional "request", how does the
> ORM create instances of it ? It doesn't know anything about request.
> You can make a helper function. Or if you really want to be a part of
> the class, make an alternative constructor:
>
>    @classmethod
>    def from_provision(cls, provision):
>         instance = cls()
>         instance.provision = provision
>         instance.perio = provision.get_current_period()
>
>     # in your view code::
>
>     header = ReportHeader.from_provision(requrest.provision)
>
> 2) The moment you wrote "locals()" all the code in your view becomes
> relevant, because you're passing all the local variables to your
> template. This seems very popular amongst some groups, but I
> personally consider this not only bad style, but error-prone and
> possibly dangerous.
>
> 3) Catching exceptions like that most likely masks the real problem
> (Like the fact, that creating an instance of ReportHeader failed, due
> to buggy constructor). After all, you're rendering the template even
> if you failed to fetch the data you most likely need to render it.
>
> --
> Łukasz Rekucki

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Error 'long' has no attribute provision [RSS Feed]

Board footer

Moderator control

Enjoy the 20th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support