On 5/8/2009 5:58 PM, Lee Hinde wrote:
> On Fri, May 8, 2009 at 5:12 PM, Lee Hinde <leehi...@gmail.com> wrote:
>> On Fri, May 8, 2009 at 4:48 PM, George Song <geo...@damacy.net> wrote:
>>> On 5/8/2009 4:32 PM, Lee Hinde wrote:
>>>> Hi;
>>>>
>>>> I get the error below at a point I'm trying to add 1 to an int field.
>>>> I'm understanding the error that django doesn't know what the type is
>>>> for sn.
>>>>
>>>>
>>>> Model:
>>>>
>>>> class Sequence_Number(models.Model):
>>>>     Sequence_Name = models.CharField(max_length=100)
>>>>     Next_Sequence_Number = models.IntegerField()
>>>>     LastUpdate = models.DateField(auto_now = True)
>>>>     dupefixed = models.DateField(blank=True,null=True)
>>>>
>>>>     def __unicode__(self):
>>>>         return self.Sequence_Name
>>>>
>>>> Sequence Number function:
>>>>
>>>> @transaction.autocommit
>>>> def get_SequenceNumber(itemToSequnce):
>>>>     try:
>>>>         sn = Sequence_Number.objects.get(Sequence_Name=itemToSequnce)
>>>>     except Sequence_Number.DoesNotExist:
>>>>         sn = Sequence_Number(Sequence_Name=itemToSequnce)
>>>>     next_number = sn.Next_Sequence_Number
>>>>     sn.Next_Sequence_Number += 1
>>>>     sn.save()
>>>>     return next_number
>>>>
>>>> def buildSlug(slug):
>>>>     slug = "slug__%s" % (slug)
>>>>     next_Num = get_SequenceNumber(slug)
>>>>     slug_with_number = "%s%d" % (slug,next_Num)
>>>>     return slug_with_number
>>>>
>>>>
>>>>
>>>> Traceback:
>>>>
>>>>   File "<console>", line 2, in <module>
>>>>   File 
>>>> "/Users/leehinde/Documents/Clients/RecEnrollTNV/RecEnroll/recenrolltnv/../recenrolltnv/recenroll/models.py",
>>>> line 314, in save
>>>>     self.Slug = buildSlug(start)
>>>>   File 
>>>> "/Users/leehinde/Documents/Clients/RecEnrollTNV/RecEnroll/recenrolltnv/../recenrolltnv/recenroll/models.py",
>>>> line 20, in buildSlug
>>>>     next_Num = get_SequenceNumber(slug)
>>>>   File "/Library/Python/2.5/site-packages/django/db/transaction.py",
>>>> line 223, in _autocommit
>>>>     return func(*args, **kw)
>>>>   File 
>>>> "/Users/leehinde/Documents/Clients/RecEnrollTNV/RecEnroll/recenrolltnv/../recenrolltnv/recenroll/models.py",
>>>> line 14, in get_SequenceNumber
>>>>     sn.Next_Sequence_Number += 1
>>>> TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
>>> {{{
>>> try:
>>>     sn = Sequence_Number.objects.get(Sequence_Name=itemToSequnce)
>>> except Sequence_Number.DoesNotExist:
>>>     sn = Sequence_Number(Sequence_Name=itemToSequnce)
>>> next_number = sn.Next_Sequence_Number
>>> }}}
>>>
>>> Well, in your except block, you're just instantiating a Sequence_Number
>>> object, so of course it doesn't have the Next_Sequence_Number attribute yet.
>>>
>>> Did you mean to do:
>>> {{{
>>> Sequence_Number.objects.create(Sequence_Name=itemToSequnce)
>>> }}}
>>> ?
>>>
>> I may mean that. :-) I was trying to emulate the documentation around
>> the get_or_create method.
>>
>> The documentation around transaction implied that the transaction
>> would be closed when the object was saved,, I wasn't sure if
>> get_or_create would end the transaction.
>>
>> In any case I get the same error if I seed the  Next_Sequence_Number.
>>        sn = 
>> Sequence_Number(Sequence_Name=itemToSequnce,Next_Sequence_Number=0)
>> or
>>        
>> Sequence_Number.objects.create(Sequence_Name=itemToSequnce,Next_Sequence_Number=0)
>>
> 
> 
> 
> I moved everything out to the shell and it worked. That made me think
> that there must be a typo or somesuch in what I'd originally done. I
> also may not understand mutability in Python just yet. After futzing
> around some I ended up with this and it works now:
> 
> @transaction.autocommit
> def get_SequenceNumber(itemToSequnce):
>     try:
>         sn = Sequence_Number.objects.get(Sequence_Name=itemToSequnce)
>     except Sequence_Number.DoesNotExist:
>         sn = 
> Sequence_Number.objects.create(Sequence_Name=itemToSequnce,Next_Sequence_Number=0)
>     next_number = sn.Next_Sequence_Number
>     sn.Next_Sequence_Number += 1
>     sn.save()
>     return next_number
> 
> def buildSlug(input_data):
>     slug = "%s__" % (input_data)
>     print "slug: %s"%(slug)
>     next_Num = get_SequenceNumber(slug)
>     print "nn: %d"%(next_Num)
>     slug_with_number = "%s%d" % (slug,next_Num)
>     print "slugwnum: %s"%(slug_with_number)
>     return slug_with_number
> 
> 
> George, thanks for the help. It's greatly appreciated.

I'm glad it worked out for you. While I don't know your specific use 
case, it feels more natural to me to do something like this:

{{{
Sequence_Number.next_for(itemToSequence)
}}}

So I would encourage you to promote the get_SequenceNumber utility 
function to a Sequence_Number class method.

-- 
George

--~--~---------~--~----~------------~-------~--~----~
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 at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to