Re: TypeError unsupported operand type(s) for *: 'NoneType' and 'int'

2019-04-07 Thread omar ahmed
ok i understood 
thanks for help

On Sunday, April 7, 2019 at 11:58:50 AM UTC+2, Roger Gammans wrote:
>
> Omar,
>
> When posting question to the list it's good practice to practice tell us a 
> little bit about what your trying to do, and how what
> actions cause the error to occur.
>
> In this case though you only have a (single line in CalcPoints) which uses 
> the '*' operator.
>
> On Sat, 2019-04-06 at 10:35 -0700, omar ahmed wrote:
>
> models.py
> def CalcPoints(self):
> return self.won*3 + self.draw
>
>
>
> The python documentation has this to say about the '*' operator
> *(from: *
> https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations
> )
>
> *The * (multiplication) operator yields the product of its arguments. The 
> arguments must either both be numbers, or one argument must be an integer 
> and the other must be a sequence. In the former case, the numbers are 
> converted to a common type and then multiplied together. In the latter 
> case, sequence repetition is performed; a negative repetition factor yields 
> an empty sequence.*
>
> Since you've provided a literal integer ( the '3') as one of your 
> arguments, we can deduce the None value
> is in self.won. Since you have 'declared' self.won as an IntegerField; we 
> can see that as in the
> normal case we should satisfy the first clause of the reference, "*arguments 
> must either both be numbers"*
> however so some reason self.won is None in your case.
>
> Without seeing the rest of your code it's impossible to guess accurately 
> but my instinct is your haven't
> initialised your instance from the database (or set a won value when 
> creating the object).
>
> If you expected to have a default value you need to set one where you set 
> up self.won see 
> : https://docs.djangoproject.com/en/2.2/ref/models/fields/#default
>
> HTH,
>
> -- 
>
> Roger Gammans >
>
>

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/d152c3b3-591d-4012-b18a-d4014156c8a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: TypeError unsupported operand type(s) for *: 'NoneType' and 'int'

2019-04-07 Thread Roger Gammans
Omar,

When posting question to the list  it's good practice to  practice tell
us a little bit about what your trying to do, and how what
actions cause the error to occur.

In this case though you only have a (single line in CalcPoints) which
uses the '*' operator.

On Sat, 2019-04-06 at 10:35 -0700, omar ahmed wrote:
> models.py
> def CalcPoints(self):
> return self.won*3 + self.draw


The python documentation has this to say about the '*' operator
(from: 
https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations
)

The * (multiplication) operator yields the product of its
arguments.  The
arguments must either both be numbers, or one argument must be an
integer and
the other must be a sequence. In the former case, the numbers are
converted to a
common type and then multiplied together.  In the latter case, sequence
repetition is performed; a negative repetition factor yields an empty
sequence.

Since you've provided a literal integer ( the '3') as one of your
arguments, we can deduce the None value
is in self.won. Since you have 'declared' self.won as an
IntegerField;  we can see that as in the
normal case we should satisfy the first clause of the reference,
"arguments must either both be numbers"
however so some reason self.won is None in your case.

Without seeing the rest of your code it's impossible to guess
accurately but my instinct is your haven't
initialised your instance from the database (or set a won value when
creating the object).

If you expected to have a default value you need to set one where you
set up self.won see 
:  https://docs.djangoproject.com/en/2.2/ref/models/fields/#default

HTH,


-- 
Roger Gammans 

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/ea23ce16123320b41ad5f468ebb742f1bcb25b37.camel%40gammascience.co.uk.
For more options, visit https://groups.google.com/d/optout.


TypeError unsupported operand type(s) for *: 'NoneType' and 'int'

2019-04-06 Thread omar ahmed
models.py
class Club(models.Model):
league_names = models.ForeignKey(League, on_delete= models.CASCADE, 
related_name='club')
name = models.CharField(max_length=100)
logo = models.ImageField(upload_to='media/core', max_length=255, null=True, 
blank=True)
won = models.IntegerField()
draw = models.IntegerField()
lost = models.IntegerField()
goal_for = models.IntegerField()
goal_against = models.IntegerField()
club_position = models.IntegerField()

def CalcPoints(self):
return self.won*3 + self.draw


admin.py

class ClubAdmin(admin.ModelAdmin):
list_display = ['league_names', 'name', 'logo', 'won', 'draw', 'lost', 
'total_points', 'goal_for', 'goal_against', 'goal_diff']
readonly_fields = ('total_points', 'goal_diff',)

admin.site.register(Club, ClubAdmin)

total_points = property(CalcPoints)

def GoalDiff(self):
return self.goal_for - self.goal_against

goal_diff = property(GoalDiff)


def __str__(self):
return self.name

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8ccf9b0f-da36-470e-9489-a4edd264943a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

2009-05-08 Thread Lee Hinde

On Fri, May 8, 2009 at 6:51 PM, George Song <geo...@damacy.net> wrote:
>
> 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 "", line 2, in 
>>>>>   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
>> th

Re: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

2009-05-08 Thread George Song

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 "", line 2, in 
>>>>   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

Re: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

2009-05-08 Thread Lee Hinde

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 "", line 2, in 
>>>   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.

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

2009-05-08 Thread Lee Hinde

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 "", line 2, in 
>>   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 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
-~--~~~~--~~--~--~---



Re: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

2009-05-08 Thread George Song

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 "", line 2, in 
>   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)
}}}
?

-- 
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
-~--~~~~--~~--~--~---



TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

2009-05-08 Thread Lee Hinde

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 "", line 2, in 
  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'

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---