On Fri, May 8, 2009 at 4:48 PM, George Song <[email protected]> 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)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to