I'm generating some test data for my models.
I have Country, States and Citys, this are descending from
models.Model.
Example:
class Country(models.Model):
name =
models.CharField('nombre',maxlength=50,blank=False,db_index=True,unique=True)
language = models.CharField('lenguaje',maxlength=5,blank=False)
zipcode =
models.CharField('indicativo',maxlength=10,blank=False,unique=True)
def __str__(self):
return '%s - id: %s' % (self.name, self.id)
class Admin:
pass
class Meta:
verbose_name = 'pais'
verbose_name_plural = 'paises'
db_table = 'jhonWebCountry'
ordering = ['name']
Now, I have a base class:
class BaseContent(models.Model):
createdOn = models.DateTimeField('fecha de
creación',editable=False,auto_now_add=True)
modifedOn = models.DateTimeField('fecha de
modificación',editable=False,auto_now=True)
tags =
models.CharField('etiquetas',maxlength=300,blank=False)
title =
models.CharField('titulo',maxlength=50,blank=False,db_index=True)
userId = models.SlugField(prepopulate_from=('title',
'title'),blank=False)
content = models.TextField('contenido')
author =
models.CharField('autor',maxlength=50,blank=False,db_index=True,default='1')
status =
models.IntegerField('estado',default=1,choices=ESTATUS_CHOICES)
def __str__(self):
return '%s - Id: %s' % (self.title, self.id)
def html(self):
return content
class Admin:
pass
class Meta:
verbose_name = 'contenido'
db_table = 'jhonWebContent'
get_latest_by = 'modifedOn'
ordering = ['title']
And in a separate module, I have:
from django.db import models
from jhonWeb.core.models import BaseContent
# Copyrigth Soluciones Vulcano Ltda www.solucionesvulcano.com
# Core: Modulo de bloging y comentarios...
""" Clase de blog o diario/bitacora """
class Blog(BaseContent):
def get_absolute_url(self):
return "/diario/%s/" % self.userId
class Admin:
pass
class Meta:
verbose_name = 'diario'
db_table = 'jhonWebBlog'
So, I get the tables created in sqlite, I see the admin interface of
this, I have the urls settings. All fine.
So, I go to the command line:
>>blog = Blog(tags='Producto1',title='Blog1',content='Contenido Blog')
>>blog.id
Print nothing
>>blog.save()
>>blog.id
1
And I query:
>>Blog.objects.all().count()
0
or
>>blogs = Blog.objects.all()
>>blogs.count()
0
I go to the admin interface, I click on Blogs, I *SEE* the created
blog, I clik on it and get a 404 error page. I return to the command
line and try:
>>blog = Blog.objects.filter(id=1)
>>blog.id
Traceback (most recent call last):
File "<console>", line 1, in ?
AttributeError: 'QuerySet' object has no attribute 'id'
I try with Country, State and City, both in command line and Admin
pages, and are fine...
Weird...
So, I remove the subclassing, copy the fields from BaseContent to Blog,
then recreate the database and try.
Work fine...
My project have this layout:
-root
-root\core = here BaseContent
-root\blogs = here Blog(BaseContent). I import this way: from
jhonWeb.core.models import BaseContent
blog.save()
> class Blog(BaseContent):
>
> def get_absolute_url(self):
> return "/diario/%s/" % self.userId
>
> class Admin:
> pass
>
> class Meta:
> verbose_name = 'diario'
> db_table = 'jhonWebBlog'
>
[...]
>
> So, I remove the subclassing, copy the fields from BaseContent to Blog,
> then recreate the database and try.
>
> Work fine...
Model inheritance does not work in the development version. Because of
some changes in the magic-removal branch things that did work in 0.91
have stopped working. Don't use model inheritance at the moment.
This does not mean you have to repeat all you code, though. You can fake
inheritance the way you do in languages without OO. Back when dinosaurs
roamed the earth and everybody programmed in C, if you wanted to have
one struct "inherit" from another, the child would just contain a
pointer to the parent. Similarly here,
class Blog(models.Model):
parent_model = models.ForeignKey(BaseContent, unique = True)
...
Now you have to access the parent objects via blog_obj.parent_model.foo,
unfortunately, but that is usually survivable.
Work is ongoing to get model inheritance working the development
version. I started writing code last weekend to do this, but it's at
least a couple of weeks away from even being submitted to the
developer's list for review, so it's not going to appear in the trunk
anytime soon. For now, just say "no". And use the foreign key trick if
you really need to.
Regards,
Malcolm
Maybe out a warning in the docs?
Is good idea then go for 1-1 relationships?