Django model manager use_for_related_fields and ModelAdmin relationships -


I am having trouble managing my model manager properly while using the admin interface. Basically, I have two models Are:

  class employee (model. Model): objects = model. EmployeeManager () username = models.CharField (max_length = 45, primary_key = true). . Category Eotm (models.Model): Month date # Model = Model. Datafield () employee = model Faircy (employee). . .  

And I have a employee manager class which overrides the get () method, something like this:

  Class Employee Manager (models.Manager): use_for_related_fields = True def (self, * logic, ** keywords): Try: return super (employee manager, self). Except self * model (* logic, ** keyword) .DoesNotExist: # If there is no employee matching query, try a LDAP look and create # example model for the result, if any. In fact, the idea is that they have been created with information in the  employee  objects in Active Directory, if they are not present in the database, it is better than my application code But when I tried to create a Django admin page for the  Eotm  model, things were not very good, I had a  TextInput  with the widget  The default widget for the ForeignKey  field has changed. Individual users can type a username (as  username  is the primary key). In principle, it should call  EmployeeManager.get (username = 'whatever') , which will either return a  employee  like default manager or create one and It was possible to return it was not already existed The problem is that my manager is not being used. 

I do not find anything in the DJ ango documentation about using custom managers sections and admin site. I was talking about specifying a custom manager for classes for ModelAdmin , but it does not really help because I do not want to change the model represented by ModelAdmin Yes class, but on which it is related.

I do not understand what you are trying to do here, but you You can use a custom form for your EOTM model:

Import from imported form from the EotmAdminForm class EotmAdmin (models.ModelAdmin): forms = EotmAdminForm # forms.py Import from imported django models, imported eater, employee class EotmAdminForm (forms.ModelForm) class meta: model = Eotm def cleanemployee (self): user name = self.cleaned_data ['employee'] return Employee.get (user Ta name = username)

In the theory, it should work, I have not tested it.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -