def get_related_to(model_class, target_model):
related = []
if model_class == target_model:
return ['email']
for field in model_class._meta.fields:
if isinstance(field, ForeignKey):
if field.rel.to == target_model:
related = [field.name, 'email']
else:
related.extend(get_related_to(field.rel.to, target_model))
return related
python类ForeignKey()的实例源码
def _get_foreign_key_classes(m):
return set([field.rel.to for field in m._meta.fields if isinstance(field, ForeignKey)])
def serialize_model(self):
"""
Check the fields of models and convert the data
Returns: Dictionary
"""
if not hasattr(self.model, '__dict__'):
raise TypeError("Models or Dictionary is required")
response = {}
opts = self.model._meta
for field in opts.local_fields:
# for a django model, retrieving a foreignkey field
# will fail when the field value isn't set
try:
value = getattr(self.model, field.name)
except ObjectDoesNotExist:
value = None
if isinstance(value, datetime):
value = datetime_to_str(value)
if isinstance(value, timedelta):
value = timedelta_to_str(value)
if isinstance(field, ForeignKey):
fk_id = "%s_id" % field.name
if value is None:
response[fk_id] = None
else:
response[fk_id] = getattr(self.model, fk_id)
value = str(value)
response[field.name] = value
for field in opts.local_many_to_many:
value = getattr(self.model, field.name)
value = value.values_list('pk', flat=True)
response[field.name] = list(value)
return response
def add_syncable_models():
"""
Per profile, adds each model to a dictionary mapping the morango model name to its model class.
We sort by ForeignKey dependencies to safely sync data.
"""
import django.apps
from morango.models import SyncableModel
from morango.manager import SyncableModelManager
from morango.query import SyncableModelQuerySet
model_list = []
for model_class in django.apps.apps.get_models():
# several validation checks to assert models will be syncing correctly
if issubclass(model_class, SyncableModel):
name = model_class.__name__
if _multiple_self_ref_fk_check(model_class):
raise InvalidMorangoModelConfiguration("Syncing models with more than 1 self referential ForeignKey is not supported.")
try:
from mptt import models
from morango.utils.morango_mptt import MorangoMPTTModel, MorangoMPTTTreeManager, MorangoTreeQuerySet
# mptt syncable model checks
if issubclass(model_class, models.MPTTModel):
if not issubclass(model_class, MorangoMPTTModel):
raise InvalidMorangoModelConfiguration("{} that inherits from MPTTModel, should instead inherit from MorangoMPTTModel.".format(name))
if not isinstance(model_class.objects, MorangoMPTTTreeManager):
raise InvalidMPTTManager("Manager for {} must inherit from MorangoMPTTTreeManager.".format(name))
if not isinstance(model_class.objects.none(), MorangoTreeQuerySet):
raise InvalidMPTTQuerySet("Queryset for {} model must inherit from MorangoTreeQuerySet.".format(name))
except ImportError:
pass
# syncable model checks
if not isinstance(model_class.objects, SyncableModelManager):
raise InvalidSyncableManager("Manager for {} must inherit from SyncableModelManager.".format(name))
if not isinstance(model_class.objects.none(), SyncableModelQuerySet):
raise InvalidSyncableQueryset("Queryset for {} model must inherit from SyncableModelQuerySet.".format(name))
if model_class._meta.many_to_many:
raise UnsupportedFieldType("{} model with a ManyToManyField is not supported in morango.")
if not hasattr(model_class, 'morango_model_name'):
raise InvalidMorangoModelConfiguration("{} model must define a morango_model_name attribute".format(name))
if not hasattr(model_class, 'morango_profile'):
raise InvalidMorangoModelConfiguration("{} model must define a morango_profile attribute".format(name))
# create empty list to hold model classes for profile if not yet created
profile = model_class.morango_profile
_profile_models[profile] = _profile_models.get(profile, [])
# don't sync models where morango_model_name is None
if model_class.morango_model_name is not None:
_insert_model_into_profile_dict(model_class, profile)
# for each profile, create a dict mapping from morango model names to model class
for profile, model_list in iteritems(_profile_models):
syncable_models_dict = OrderedDict()
for model_class in model_list:
syncable_models_dict[model_class.morango_model_name] = model_class
_profile_models[profile] = syncable_models_dict
def export_selected_data(self,request,queryset):
ops = self.model._meta
workbook = xlwt.Workbook(encoding='utf-8')
dd = datetime.date.today().strftime('%Y%m%d')
file_name = force_text(ops.verbose_name+dd)
sheet = workbook.add_sheet(force_text(ops.verbose_name))
obj_fields = getattr(self,'export_fields',None) or self.list_display or self.fields
head_col_index = 0
for field in obj_fields:
col_name = field
try:
f = ops.get_field(field)
col_name = f.verbose_name
except Exception,e:
f = getattr(self.model,field)
if hasattr(f,'short_description'):
col_name = f.short_description
sheet.write(0,head_col_index,force_text(col_name))
head_col_index+=1
row_index = 1
for obj in queryset:
col_index = 0
for field in obj_fields:
f = field
try:
f = ops.get_field(field)
except Exception,e:
pass
v = getattr(obj,field,'')
if hasattr(v,'__call__') or callable(v):
v = v()
elif type(f) == fields.DateField:
v = v.strftime('%Y-%m-%d')
elif type(f) == fields.DateTimeField:
v = v.strftime('%Y-%m-%d %H:%M')
elif type(f) == fields.CharField and f.choices:
fc = 'get_'+field+'_display'
v = getattr(obj,fc)()
elif type(f) == related.ForeignKey:
v = str(v)
sheet.write(row_index,col_index,v)
col_index += 1
row_index += 1
response = HttpResponse(content_type='application/vnd.ms-excel')
agent = request.META.get('HTTP_USER_AGENT')
nn = smart_str(file_name)
if agent and re.search('MSIE',agent):
nn = urlquote(file_name)
response['Content-Disposition'] = 'attachment; filename=%s.xls'%nn
workbook.save(response)
return response
#self.message_user(request,'SUCCESS')