Changeset 1414
- Timestamp:
- 11/24/05 19:51:19 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/new-admin/django/core/meta/fields.py
r1361 r1414 58 58 if hasattr(self, 'original_object') and getattr(self.original_object, opts.pk.attname) == getattr(old_obj, opts.pk.attname): 59 59 return 60 raise validators.ValidationError, _("%(optname)s with this %(fieldname)s already exists.") % {'optname':capfirst(opts.verbose_name), 'fieldname':f.verbose_name} 61 60 raise validators.ValidationError, _("%(optname)s with this %(fieldname)s already exists.") % {'optname': capfirst(opts.verbose_name), 'fieldname': f.verbose_name} 62 61 63 62 class BoundField(object): … … 75 74 def original_value(self): 76 75 if self.original: 77 return self.original.__dict__[self.field.column] 76 return self.original.__dict__[self.field.column] 78 77 79 78 def __repr__(self): 80 return "BoundField:(%s, %s)" %( self.field.name, self.form_fields) 81 79 return "BoundField:(%s, %s)" % (self.field.name, self.form_fields) 82 80 83 81 # A guide to Field parameters: … … 97 95 98 96 class Field(object): 99 97 100 98 # Designates whether empty strings fundamentally are allowed at the 101 99 # database level. … … 248 246 else: 249 247 field_objs = [formfields.SelectField] 250 248 251 249 params['choices'] = self.get_choices_default() 252 250 else: … … 311 309 def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH): 312 310 "Returns a list of tuples used as SelectField choices for this field." 313 314 311 first_choice = include_blank and blank_choice or [] 315 312 if self.choices: 316 313 return first_choice + list(self.choices) 317 314 rel_obj = self.rel.to 318 return first_choice + [(getattr(x, rel_obj.pk.attname), str(x)) 315 return first_choice + [(getattr(x, rel_obj.pk.attname), str(x)) 319 316 for x in rel_obj.get_model_module().get_list(**self.rel.limit_choices_to)] 320 317 … … 327 324 def _get_val_from_obj(self, obj): 328 325 if obj: 329 return getattr(obj, self.attname) 330 else: 326 return getattr(obj, self.attname) 327 else: 331 328 return self.get_default() 332 329 … … 334 331 """ 335 332 Returns a dictionary mapping the field's manipulator field names to its 336 "flattened" string values for the admin view. Obj is the instance to extract the337 values from.333 "flattened" string values for the admin view. obj is the instance to 334 extract the values from. 338 335 """ 339 return { self.attname: self._get_val_from_obj(obj)}336 return {self.attname: self._get_val_from_obj(obj)} 340 337 341 338 def get_follow(self, override=None): … … 344 341 else: 345 342 return self.editable 346 343 347 344 def bind(self, fieldmapping, original, bound_field_class=BoundField): 348 345 return bound_field_class(self, fieldmapping, original) 349 346 350 347 class AutoField(Field): 351 348 empty_strings_allowed = False … … 387 384 def __init__(self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs): 388 385 self.auto_now, self.auto_now_add = auto_now, auto_now_add 389 #HACKs : auto_now_add/auto_now should be done as a default or a pre_save... 386 #HACKs : auto_now_add/auto_now should be done as a default or a pre_save... 390 387 if auto_now or auto_now_add: 391 388 kwargs['editable'] = False … … 455 452 456 453 def flatten_data(self,follow, obj = None): 457 val = self._get_val_from_obj(obj) 454 val = self._get_val_from_obj(obj) 458 455 date_field, time_field = self.get_manipulator_field_names('') 459 456 return {date_field: (val is not None and val.strftime("%Y-%m-%d") or ''), … … 659 656 660 657 def flatten_data(self,follow, obj = None): 661 val = self._get_val_from_obj(obj) 662 return {self.attname: (val is not None and val.strftime("%H:%M:%S") or '')} 658 val = self._get_val_from_obj(obj) 659 return {self.attname: (val is not None and val.strftime("%H:%M:%S") or '')} 663 660 664 661 class URLField(Field): … … 785 782 786 783 def flatten_data(self, follow, obj = None): 787 new_data = {} 784 new_data = {} 788 785 if obj: 789 786 get_list_func = getattr(obj, 'get_%s_list' % self.rel.singular) … … 792 789 new_data[self.name] = ",".join([str(id) for id in instance_ids]) 793 790 else: 794 new_data[self.name] = instance_ids 791 new_data[self.name] = instance_ids 795 792 else: 796 793 # In required many-to-many fields with only one available choice, … … 870 867 class BoundFieldLine(object): 871 868 def __init__(self, field_line, field_mapping, original, bound_field_class=BoundField): 872 self.bound_fields = [field.bind(field_mapping, original, bound_field_class) 873 for field in field_line] 874 869 self.bound_fields = [field.bind(field_mapping, original, bound_field_class) for field in field_line] 870 875 871 def __iter__(self): 876 872 for bound_field in self.bound_fields: 877 873 yield bound_field 878 879 874 880 875 def __len__(self): 881 876 return len(self.bound_fields) 882 877 883 878 class FieldLine(object): 884 879 def __init__(self, field_locator_func, linespec): … … 894 889 for field in self.fields: 895 890 yield field 896 891 897 892 def __len__(self): 898 893 return len(self.fields) 899 894 900 895 class BoundFieldSet(object): 901 896 def __init__(self, field_set, field_mapping, original, bound_field_line_class=BoundFieldLine): 902 897 self.name = field_set.name 903 898 self.classes = field_set.classes 904 self.bound_field_lines = [ field_line.bind(field_mapping,original, bound_field_line_class) 905 for field_line in field_set] 906 899 self.bound_field_lines = [field_line.bind(field_mapping,original, bound_field_line_class) for field_line in field_set] 900 907 901 def __iter__(self): 908 902 for bound_field_line in self.bound_field_lines: 909 903 yield bound_field_line 910 904 911 905 def __len__(self): 912 906 return len(self.bound_field_lines) 913 907 914 908 class FieldSet(object): 915 909 def __init__(self, name, classes, field_locator_func, line_specs): … … 917 911 self.field_lines = [FieldLine(field_locator_func, line_spec) for line_spec in line_specs] 918 912 self.classes = classes 919 913 920 914 def __repr__(self): 921 915 return "FieldSet:(%s,%s)" % (self.name, self.field_lines) … … 943 937 self.save_on_top = save_on_top 944 938 self.list_select_related = list_select_related 945 939 946 940 def get_field_sets(self, opts): 947 941 if self.fields is None: 948 942 field_struct = ((None, { 949 'fields': [f.name for f in opts.fields + opts.many_to_many if f.editable and not isinstance(f, AutoField)]950 }),)943 'fields': [f.name for f in opts.fields + opts.many_to_many if f.editable and not isinstance(f, AutoField)] 944 }),) 951 945 else: 952 946 field_struct = self.fields 953 954 947 new_fieldset_list = [] 955 948 for fieldset in field_struct: 956 949 name = fieldset[0] 957 950 fs_options = fieldset[1] 958 classes = fs_options.get('classes', ())951 classes = fs_options.get('classes', ()) 959 952 line_specs = fs_options['fields'] 960 new_fieldset_list.append(FieldSet(name, classes, opts.get_field, line_specs) )953 new_fieldset_list.append(FieldSet(name, classes, opts.get_field, line_specs)) 961 954 return new_fieldset_list 962 963
