Thursday, June 24, 2010

Re: Content types and interaction with DeferredAttribute models

I have the same problem.  It looks like the content types app will return the content type of the original model when asked for one since revision 10523 (see ticket #10738), but that an additional content type is still created.

Is the creation of the additional content type necessary?

http://code.djangoproject.com/ticket/10738

On Wed, Nov 25, 2009 at 2:32 PM, Giovannetti, Mark <Mark.Giovannetti@nrcan-rncan.gc.ca> wrote:

FYI:

Trying to run unittests.

Using the .only() method on a queryset causes the content types
machinery to find and attempt to use a model with Deferred attributes.

This function (with print and verbosity added):

def update_contenttypes(app, created_models, verbosity=2, **kwargs):
   """
   Creates content types for models in the given app, removing any
model
   entries that no longer have a matching model class.
   """
   verbosity=2
   ContentType.objects.clear_cache()
   content_types =
list(ContentType.objects.filter(app_label=app.__name__.split('.')[-2]))
   app_models = get_models(app)
   #print "app, app_models", app, app_models
   if not app_models:
       return
   for klass in app_models:
       opts = klass._meta
       #print "klass, opts", klass, opts, opts.__dict__
       try:
           ct = ContentType.objects.get(app_label=opts.app_label,
                                        model=opts.object_name.lower())
           content_types.remove(ct)
       except ContentType.DoesNotExist:
           ct = ContentType(name=smart_unicode(opts.verbose_name_raw),
               app_label=opts.app_label,
model=opts.object_name.lower())
           print ct.name, len(ct.name), ct.app_label, ct.model
           ct.save()
           if verbosity >= 2:
               print "Adding content type '%s | %s'" % (ct.app_label,
ct.model)

[...]

while running unittests, gives this output and error.

Adding content type 'contact | webaddress'
postal address 14 contact postaladdress
Adding content type 'contact | postaladdress'
postal address boundary 23 contact postaladdressboundary
Adding content type 'contact | postaladdressboundary'
contact mechanism link 22 contact contactmechanismlink
Adding content type 'contact | contactmechanismlink'

===
Output of:
           print ct.name, len(ct.name), ct.app_label, ct.model gives:

contact mechanism type_
deferred_creation_date_time_description_display_is_default_privilege_tok
en_record_deleted_record_enabled 128 contact
contactmechanismtype_deferred_creation_date_time_description_display_is_
default_privilege_token_record_deleted_record_enabled
===
Traceback (most recent call last):
 File
"C:\1mvgdocs\neodfos_workspace\neodfos\trunk\src\tools\tests\run_tests.p
y", line 245, in <module>
   test_name =
connection.creation.create_test_db(verbosity=db_verbosity,
autoclobber=autoclobber)
 File "C:\Python26\lib\site-packages\django\db\backends\creation.py",
line 336, in create_test_db
   call_command('syncdb', verbosity=verbosity, interactive=False)
 File
"C:\Python26\lib\site-packages\django\core\management\__init__.py", line
166, in call_command
   return klass.execute(*args, **defaults)
 File "C:\Python26\lib\site-packages\django\core\management\base.py",
line 222, in execute
   output = self.handle(*args, **options)
 File "C:\Python26\lib\site-packages\django\core\management\base.py",
line 351, in handle
   return self.handle_noargs(**options)
 File
"C:\Python26\lib\site-packages\django\core\management\commands\syncdb.py
", line 99, in handle_noargs
   emit_post_sync_signal(created_models, verbosity, interactive)
 File "C:\Python26\lib\site-packages\django\core\management\sql.py",
line 205, in emit_post_sync_signal
   interactive=interactive)
 File "C:\Python26\lib\site-packages\django\dispatch\dispatcher.py",
line 166, in send
   response = receiver(signal=self, sender=sender, **named)
 File
"C:\Python26\lib\site-packages\django\contrib\contenttypes\management.py
", line 28, in update_contenttypes
   ct.save()
 File "C:\Python26\lib\site-packages\django\db\models\base.py", line
410, in save
   self.save_base(force_insert=force_insert, force_update=force_update)
 File "C:\Python26\lib\site-packages\django\db\models\base.py", line
495, in save_base
   result = manager._insert(values, return_id=update_pk)
 File "C:\Python26\lib\site-packages\django\db\models\manager.py", line
177, in _insert
   return insert_query(self.model, values, **kwargs)
 File "C:\Python26\lib\site-packages\django\db\models\query.py", line
1087, in insert_query
   return query.execute_sql(return_id)
 File
"C:\Python26\lib\site-packages\django\db\models\sql\subqueries.py", line
320, in execute_sql
   cursor = super(InsertQuery, self).execute_sql(None)
 File "C:\Python26\lib\site-packages\django\db\models\sql\query.py",
line 2369, in execute_sql
   cursor.execute(sql, params)
 File "C:\Python26\lib\site-packages\django\db\backends\util.py", line
19, in execute
   return self.cursor.execute(sql, params)
psycopg2.DataError: value too long for type character varying(100)


The error can be eliminated if the .only() method is not used:

               #for subtype in type_entity.objects.all().only('slug'):
               for subtype in type_entity.objects.all():


The tests complete without error when changing the above line to not use
.only().

Other output from the various print statements in update_contenttypes
(note the last model listed):

app, app_models
<module 'os_so.contact.models' from
'C:\...\trunk\src\os_so\contact\models.pyc'>
[
<class 'os_so.contact.models.ContactMechanismType'>,
<class 'os_so.contact.models.ContactMechanism'>,
<class 'os_so.contact.models.OrganizationContactPhoneNumber'>,
<class 'os_so.contact.models.FaxNumber'>,
<class 'os_so.contact.models.MobilePhone'>,
<class 'os_so.contact.models.HomePhone'>,
<class 'os_so.contact.models.OfficePhone'>,
<class 'os_so.contact.models.EmailAddress'>,
<class 'os_so.contact.models.FTPAddress'>,
<class 'os_so.contact.models.WebAddress'>,
<class 'os_so.contact.models.PostalAddress'>,
<class 'os_so.contact.models.PostalAddressBoundary'>,
<class 'os_so.contact.models.ContactMechanismLink'>,
<class 'os_so.contact.models.ContactMechanismPurposeType'>,
<class
'os_so.contact.models.ContactMechanismType_Deferred_creation_date_time_d
escription_display_is_default_privilege_token_record_deleted_record_enab
led'>]

This model is created in the db.models.query_utils machinery
in deferred_class_factory.




--

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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.





--
=======================
Andrew D. Ball
勃安
"Ὁ θεὸς ἀγάπη ἐστίν ..." (1 Jn 4:16)


--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment