本文整理汇总了Python中django.db.models.fields.related.ManyToManyField.contribute_to_class方法的典型用法代码示例。如果您正苦于以下问题:Python ManyToManyField.contribute_to_class方法的具体用法?Python ManyToManyField.contribute_to_class怎么用?Python ManyToManyField.contribute_to_class使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.fields.related.ManyToManyField
的用法示例。
在下文中一共展示了ManyToManyField.contribute_to_class方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_m2m
# 需要导入模块: from django.db.models.fields.related import ManyToManyField [as 别名]
# 或者: from django.db.models.fields.related.ManyToManyField import contribute_to_class [as 别名]
def test_m2m(self):
"""
Tests adding/removing M2M fields on models
"""
# Create the tables
with connection.schema_editor() as editor:
editor.create_model(AuthorWithM2M)
editor.create_model(TagM2MTest)
# Create an M2M field
new_field = ManyToManyField("schema.TagM2MTest", related_name="authors")
new_field.contribute_to_class(AuthorWithM2M, "tags")
try:
# Ensure there's no m2m table there
self.assertRaises(DatabaseError, self.column_classes, new_field.rel.through)
# Add the field
with connection.schema_editor() as editor:
editor.add_field(
Author,
new_field,
)
# Ensure there is now an m2m table there
columns = self.column_classes(new_field.rel.through)
self.assertEqual(columns['tagm2mtest_id'][0], "IntegerField")
# Remove the M2M table again
with connection.schema_editor() as editor:
editor.remove_field(
Author,
new_field,
)
# Ensure there's no m2m table there
self.assertRaises(DatabaseError, self.column_classes, new_field.rel.through)
finally:
# Cleanup model states
AuthorWithM2M._meta.local_many_to_many.remove(new_field)
示例2: register
# 需要导入模块: from django.db.models.fields.related import ManyToManyField [as 别名]
# 或者: from django.db.models.fields.related.ManyToManyField import contribute_to_class [as 别名]
def register(model, field_name = None, m2m = False):
"""
This registers the model class so it can have followers
"""
from models import Follow
if model in registry:
return
registry.append(model)
related_name = 'follow_%s' % model._meta.module_name
if not field_name:
field_name = model._meta.module_name
# Create foreignkeys by default - less sql queries for lookups
if m2m:
field = ManyToManyField(
model,
related_name = related_name,
)
else:
field = ForeignKey(
model,
related_name = related_name,
blank = True,
null = True,
)
field.contribute_to_class(Follow, field_name)
setattr(model, 'followers', followers_for_object)
# We need to keep track of which fields and which kind of fields point where
model_map[model] = [related_name, field_name, m2m]
示例3: test_m2m_repoint
# 需要导入模块: from django.db.models.fields.related import ManyToManyField [as 别名]
# 或者: from django.db.models.fields.related.ManyToManyField import contribute_to_class [as 别名]
def test_m2m_repoint(self):
"""
Tests repointing M2M fields
"""
# Create the tables
with connection.schema_editor() as editor:
editor.create_model(Author)
editor.create_model(BookWithM2M)
editor.create_model(TagM2MTest)
editor.create_model(UniqueTest)
# Ensure the M2M exists and points to TagM2MTest
constraints = connection.introspection.get_constraints(
connection.cursor(), BookWithM2M._meta.get_field_by_name("tags")[0].rel.through._meta.db_table
)
if connection.features.supports_foreign_keys:
for name, details in constraints.items():
if details["columns"] == ["tagm2mtest_id"] and details["foreign_key"]:
self.assertEqual(details["foreign_key"], ("schema_tagm2mtest", "id"))
break
else:
self.fail("No FK constraint for tagm2mtest_id found")
# Repoint the M2M
new_field = ManyToManyField(UniqueTest)
new_field.contribute_to_class(BookWithM2M, "uniques")
try:
with connection.schema_editor() as editor:
editor.alter_field(Author, BookWithM2M._meta.get_field_by_name("tags")[0], new_field)
# Ensure old M2M is gone
self.assertRaises(
DatabaseError, self.column_classes, BookWithM2M._meta.get_field_by_name("tags")[0].rel.through
)
# Ensure the new M2M exists and points to UniqueTest
constraints = connection.introspection.get_constraints(
connection.cursor(), new_field.rel.through._meta.db_table
)
if connection.features.supports_foreign_keys:
for name, details in constraints.items():
if details["columns"] == ["uniquetest_id"] and details["foreign_key"]:
self.assertEqual(details["foreign_key"], ("schema_uniquetest", "id"))
break
else:
self.fail("No FK constraint for uniquetest_id found")
finally:
# Cleanup through table separately
with connection.schema_editor() as editor:
editor.remove_field(BookWithM2M, BookWithM2M._meta.get_field_by_name("uniques")[0])
# Cleanup model states
BookWithM2M._meta.local_many_to_many.remove(new_field)
del BookWithM2M._meta._m2m_cache
示例4: register
# 需要导入模块: from django.db.models.fields.related import ManyToManyField [as 别名]
# 或者: from django.db.models.fields.related.ManyToManyField import contribute_to_class [as 别名]
def register(model, field_name = None, m2m = False):
"""
This registers the model class so it can have followers,
adds a method to the model ``get_followers``,
returning the followers -
a query set of ``:class:auth.models.User`` objects
"""
if model in registry:
return
registry.append(model)
related_name = 'follow_%s' % model._meta.module_name
# Create foreignkeys by default - less sql queries for lookups
if m2m:
field = ManyToManyField(
model,
related_name = related_name,
)
else:
field = ForeignKey(
model,
related_name = related_name,
blank = True,
null = True,
)
if not field_name:
field_name = model._meta.module_name
from models import Follow
field.contribute_to_class(Follow, field_name)
model.add_to_class('get_followers', get_followers_for_object)
# We need to keep track of which fields and which kind of fields point where
model_map[model] = [related_name, field_name, m2m]