Odd subform caching problem

19 views
Skip to first unread message

Gerald Klein

unread,
Dec 15, 2014, 12:53:05 AM12/15/14
to django...@googlegroups.com
HI, I have a simple contact form with an address subform in the form of "TabularInline" where you can add an arbitrary amount of addresses, after I add a contact and an address and save, when I go to add a brand new contact and address, the address that I just added will be there and if I add the new contact it will over write not only the address but contact I added previously, so it is keeping the entire record and updating it. It won't fix itself until I stop the django server and start it again. Then I can do the whole thing again with a new record.  

I appreciate any that someone can give, thanks 

--

Gerald Klein DBA


j...@zognet.com

708-599-0352


Arch, Gentoo I3, Ranger & Vim the coding triple threat.

Linux registered user #548580 

Brought to you by the Amish Mafia

Daniel Roseman

unread,
Dec 15, 2014, 9:54:29 AM12/15/14
to django...@googlegroups.com, j...@zognet.com
On Monday, 15 December 2014 00:53:05 UTC, Gerald Klein wrote:
HI, I have a simple contact form with an address subform in the form of "TabularInline" where you can add an arbitrary amount of addresses, after I add a contact and an address and save, when I go to add a brand new contact and address, the address that I just added will be there and if I add the new contact it will over write not only the address but contact I added previously, so it is keeping the entire record and updating it. It won't fix itself until I stop the django server and start it again. Then I can do the whole thing again with a new record.  

I appreciate any that someone can give, thanks 

--

Gerald Klein DBA

 
Nobody can help you if you don't show any code. 
--
DR.

Gerald Klein

unread,
Dec 15, 2014, 11:04:13 AM12/15/14
to django...@googlegroups.com


Daniel, 
I didn't include any code as I didn't do anything unusual but if you think that would help here it is.I was hoping that there was some ordinary theoretical basis for this as I didn't do anything custom really, aside from the id generation for the "Entity" class, which I generate the id manually much like an Oracle sequence object, I do this in order to facilitate the creation of relationships between arbitrary objects. 

Any help is appreciated



***********models******************************
def get_entity_id():
    # try:
    db = mysql.connect(host="localhost", user="root", passwd="jerryklein", db="entities")
    db.query("insert entities_entityid (fake) select ' '")
    id = db.insert_id()
    return id

class Entity(models.Model):
    """ the fName field is also the company name field """
    id = models.BigIntegerField(primary_key=True, unique=True, default=get_entity_id())
    fName = models.CharField(max_length=255, blank=True, null=True)
    lName = models.CharField(max_length=255)
    entityType = models.ForeignKey(EntityType)

    class Meta:
        verbose_name = "Entity"
        verbose_name_plural = "Entities"

    def __unicode__(self):
        return "{0} {1}".format(self.fName, self.lName)


class Address(models.Model):
    addressType = models.ForeignKey(AddressType)
    entity = models.ForeignKey(Entity)
    add1 = models.CharField(max_length=50)
    add2 = models.CharField(max_length=50, blank=True, null=True)
    city = models.CharField(max_length=50)
    postalCode = models.CharField(max_length=50)

    class Meta:
        verbose_name = "Address"
        verbose_name_plural = "Addresses"

    def __unicode__(self):
        return "{0},{1},{2},{3},{4},{5}".format(self.addressType, self.add1, self.add2, self.city, self.postalCode)




class State(models.Model):
    name = models.CharField(max_length=50)
    abrev = models.CharField(max_length=50)

    class Meta:
        verbose_name = "State"
        verbose_name_plural = "States"

    def __unicode__(self):
        return "{0}".format(self.name)


class Country(models.Model):
    name = models.CharField(max_length=50)

    class Meta:
        verbose_name = "Country"
        verbose_name_plural = "Countries"

    def __unicode__(self):
        return "{0}".format(self.Name)


class ForeignAddress(Address):
    province = models.CharField(max_length=50)
    country = models.ForeignKey(Country)

    class Meta:
        verbose_name = "Foreign Address"
        verbose_name_plural = "Foreign Addresses"

    def __unicode__(self):
        return "{0},{1},{2},{3},{4},{5}".format(self.add1, self.add2, self.city, self.postalCode, self.province,
                                                self.country)


class USAddress(Address):
    state = models.ForeignKey(State)

    class Meta:
        verbose_name = "Address"
        verbose_name_plural = "Addresses"

    def __unicode__(self):
        return "{0},{1},{2},{3},{4},{5}".format(self.addressType, self.add1, self.add2, self.city, self.state, self.postalCode)




***************below is admin classes*********************************

class AddressTypeAdmin(admin.ModelAdmin):
    list_display = ('name', 'description', )
    ordering = ('name', )


admin.site.register(AddressType, AddressTypeAdmin)


class USAddressAdmin(admin.TabularInline):
    actions = ['delete_selected']
    model = USAddress
    extra = 1


class ForeignAddressAdmin(admin.TabularInline):
    model = ForeignAddress
    extra = 1

class EntityAdmin(admin.ModelAdmin):
    inlines = [USAddressAdmin, ForeignAddressAdmin]
    list_display = ('fName', 'lName', 'entityType')
    ordering = ('lName', )
    exclude = ['id']


admin.site.register(Entity, EntityAdmin)

--
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...@googlegroups.com.
To post to this group, send email to django...@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/bdaf77eb-7862-428b-923f-bb4c44c5dad0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Daniel Roseman

unread,
Dec 15, 2014, 12:20:11 PM12/15/14
to django...@googlegroups.com, j...@zognet.com
On Monday, 15 December 2014 11:04:13 UTC, Gerald Klein wrote:


Daniel, 
I didn't include any code as I didn't do anything unusual but if you think that would help here it is.I was hoping that there was some ordinary theoretical basis for this as I didn't do anything custom really, aside from the id generation for the "Entity" class, which I generate the id manually much like an Oracle sequence object, I do this in order to facilitate the creation of relationships between arbitrary objects. 

But that "aside" is of course the entire problem.

class Entity(models.Model):
    """ the fName field is also the company name field """
    id = models.BigIntegerField(primary_key=True, unique=True, default=get_entity_id())

Leaving aside whatever reasons you have for doing this very odd thing, the error is clear: you've called the get_entity_id function in the definition, which means it is executed at first import time, so every instance of Entity gets the same ID as its default. If you're using a function to provide a default value, you must always pass the *callable*, like this:

    id = models.BigIntegerField(primary_key=True, unique=True, default=get_entity_id)

ie without the calling brackets.

But I strongly recommend you don't do this. Whatever you mean by "facilitating the creation of relationships between arbitrary objects", there is almost certainly a better way to do it in Django.
--
DR.

Gerald Klein

unread,
Dec 15, 2014, 12:45:47 PM12/15/14
to django...@googlegroups.com
if I remove the parens, when I submit to create the new entity is throws a validation error that does not specify the field that is wrong, "Please correct the error below."

--jerry

--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

Collin Anderson

unread,
Dec 16, 2014, 1:02:06 PM12/16/14
to django...@googlegroups.com, j...@zognet.com
Hi,

It's probably better to handle the default in the save() method.

def save(self, **kwargs):
   
if not self.id:
       
self.id = get_entity_id()
   
super(Entity, self).save(**kwargs)

Or, better yet, just remove the "id = models.BigIntegerField" line completely and let django give you a models.AutoField which should properly auto-increment.

Collin
Reply all
Reply to author
Forward
0 new messages