def clean(self): cleaned_data = super(ObjectsForm, self).clean() # check object's key key = cleaned_data["key"] if not key: cleaned_data["key"] = generate_key(self) else: # Check the key. for model in model_mapper.get_objects_models(): if model == self.instance.__class__: # Models will validate unique values of its own, # so we do not validate them here. continue try: record = model.objects.get(key=key) except Exception, e: continue error = forms.ValidationError( "The key '%(value)s' already exists in model %(model)s.", code="unique", params={ "value": key, "model": model.__name__ }) raise forms.ValidationError({"key": error})
def get_all_objects(): """ Get all objects that can be put in player's pockets. """ choices = [] for model in model_mapper.get_objects_models(): objects = model.objects.all() choices.extend([(obj.key, obj.name + " (" + obj.key + ")") for obj in objects]) return choices
def get_all_objects(): """ Get all objects. """ choices = [] for data in model_mapper.get_objects_models(): objects = data.objects.all() choices.extend([(obj.key, obj.name + " (" + obj.key + ")") for obj in objects]) return choices
def reload(self): """ Reload data. """ self.clear() # Get model names. data_settings_list = [] data_settings_list.extend(model_mapper.get_objects_models()) data_settings_list.extend(model_mapper.get_additional_data_models()) for data_settings in data_settings_list: try: for record in data_settings.objects.all(): # Add key's model name. key = record.serializable_value("key") if key not in self.key_model: self.key_model[key] = [] self.key_model[key].append(data_settings.__name__) except Exception, e: print("Load model error: %s" % e)
if self.instance.id: index = int(self.instance.id) else: try: # Get last id. query = self.Meta.model.objects.last() index = int(query.id) index += 1 except Exception, e: pass cleaned_data["key"] = self.instance.__class__.__name__ + "_" + str( index) else: # Check the key. for model in model_mapper.get_objects_models(): if model == self.instance.__class__: # Models will validate unique values of its own, # so we do not validate them here. print("model == self.instance") continue try: record = model.objects.get(key=key) print("record: %s" % record) except Exception, e: continue error = forms.ValidationError( "The key '%(value)s' already exists in model %(model)s.", code="unique",