I'm trying to create a many2many field inside a Contact model that would have the same values like company_ids inside a Users model at all times.
I was looking for the anwserws but the odoo docs have like two sentences about this topic and do not explain this firmly.
Other sources seem to contradict themselfs because some says that related stored field do not update while other says that they do.
And after all I don't know the syntax for creating one myself because dosc are so poorly written.
I have this piece of code:
# -*- coding: utf-8 -*-
from odoo import models, fields, api
class custom_partner_fields(models.Model):
_inherit = 'res.partner'
company_ids = fields.Many2many(comodel_name='res.company', string='Related users Allowed companies', readonly=True)
It is creating field inside Contact model and now I need something to fill it in. Preferably without using onchange methods or automatic actions (I have automatic action right now - created via developer UI).
Just set the field in partner record after creating or editing the user record
__inherit = 'res.users'
#api.model
def create(self, vals):
res = super(TheClassName, self).create(vals)
res.partner_id.company_ids = res.company_ids
return res
#api.multi
def write(self, vals):
super(TheClassName, self).write(vals)
# if we edited company_ids we edit partners too
if 'company_ids' in vals:
# use mapped to call write one time good for performance
self.mapped('partner_id').write({'company_ids': vals['company_ids']})
return True
So when ever you edit company_ids in user you do the same for related partner.
This will work for new user for sure but I think you need to handle existing users
by a script or some solution to fill up the field.
Related
I have a form which enables a user to register on our website. Now I need to export all the data to excel, so I turned towards the import-export package. I have 3 models, Customer, Reference and Contact. The latter two both have a m2m with Customer. I also created Resources for these models. When I use Resource().export() at the end of my done() method in my form view, it exports all existing objects in the database, which is not what I want.
I tried googling this and only got one result, which basically says I need to use before_export(), but I can't find anywhere in the docs how it actually works.
I tried querying my customer manually like:
customer = Customer.objects.filter(pk=customer.id)
customer_data = CustomerResource().export(customer)
which works fine but then I'm stuck with the related references and contacts: reference_data = ReferenceResource().export(customer.references) gives me an TypeError saying 'ManyRelatedManager' object is not iterable. Which makes sense because export() expects an queryset, but I'm not sure if it's possible getting it that way.
Any help very appreciated!
One way is to override get_queryset(), you could potentially try to load all related data in a single query:
class ReferenceResource(resources.ModelResource):
def __init__(self, customer_id):
super().__init__()
self.customer_id = customer_id
def get_queryset(self):
qs = Customer.objects.filter(pk=self.customer.id)
# additional filtering here
return qs
class Meta:
model = Reference
# add fields as appropriate
fields = ('id', )
To handle m2m relationships, you may be able to modify the queryset to add these additional fields.
This isn't the complete answer but it may help you make progress.
I need to generate several textareas to be filled by users and submitted back to the model. However, I think they need all to have different names (am i correct?)
How can I generate random names for each textarea and how can the model get the data within one it gets the POST request?
Thanks!
EDIT: I was hoping to use the randomly generated name as a way to identify the content and to save them in the database
It's hard to give you a good answer here because you haven't indicated what you're actually trying to achieve. You could add a 1000 text fields to your form, but if they don't correlate somehow to data on your model, there's not much point, and you've neglected that crucial piece of information.
Still, on a very basic level, you can add the additional textareas to your form like so:
class MyModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
for i in range(1, 11):
self.fields['mytextarea%d' % i] = forms.CharField(label='Textarea %d' % i, widget=forms.Textarea)
UPDATE
Based on your comment, and the fact that you're intending to actually store and retrieve the textarea fields at a later point, you should create a separate model for them and link it to your main model through a one-to-many relationship. For example:
class PrimaryModel(models.Model):
... # Stuff here
class TextareaModel(models.Model):
myprimarymodel = models.ForeignKey(PrimaryModel)
mytextarea = models.TextField()
It's hard to give a good example since you haven't indicated anything about what your models look like, but the basic idea is that you have a model that contains nothing but a foreign key to the main model and a textarea field. You can then treat these textareas as inlines with via a model formset.
I'm using SeparatedValuesField to keep track of a list of strings as explained by http://davidcramer.posterous.com/code/181/custom-fields-in-django.html and many posts here on SO that recommend this as the right way to store a list of strings.
class UserProfile(models.Model):
user = models.OneToOneField(User)
device_ids = SeparatedValuesField(blank=True, null=True, default=[])
It is working fine in my application, I can add device ids and view them in the admin interface as expected.
user_profile.device_ids = ['666666-D849-524F-6984-7E9B2D768546']
But the problem is in the admin interface, when I open up the detail page for a UserProfile object, the admin interface itself is adding extra values to my device_ids field.
For example, my application inserts a value into the field, and when I view it in the admin interface it looks like this:
[u'666666-D849-524F-6984-7E9B2D768546']
Then I change some other random property on my UserProfile object and save it using the built in django admin interface save button.
When I open the UserProfile object detail page up again for my object, that value now looks like this:
[u"[u'666666-D849-524F-6984-7E9B2D768546']"]
If I repeat this process of just hitting save then opening up this detail page, it will continue nesting the actual value with u"[ ] characters.
Is there something I can do to change this functionality? Should I be storing the list of strings in a different way?
I had the same exact problem, and finally figured it out thanks to these two different answers.
What you need in this situation is a custom widget that renders the list of strings as a string. This widget needs to be tied to the custom field:
class FlattenListWidget(forms.TextInput):
def render(self, name, value, attrs=None):
if not value is None:
value = ','.join(str(v) for v in value)
return super(FlattenListWidget, self).render(name, value, attrs)
class UserProfileAdminForm(forms.ModelForm):
class Meta:
model = UserModel
widgets = {
'device_ids': FlattenListWidget(),
}
class UserProfileAdmin(admin.ModelAdmin):
form = UserProfileAdminForm
Hope that works. I had different model names in my code but managed to get a text input containing a comma separated list of values from my custom field.
If you want to join the strings with a comma and a space value = ', '.join(str(v) for v in value), remember to strip() the value in the custom field's get_db_prep_value method. Otherwise the spaces will be saved back to database.
To use the custom field in list_display you can add a custom field to the admin model:
class UserProfileAdmin(admin.ModelAdmin):
form = UserProfileAdminForm
list_display = ('ids_list',)
def ids_list(self, obj):
if obj.device_ids:
return ', '.join(str(i) for i in obj.device_ids)
return None
Oiva Eskola's answer looks fine (haven't tried it, though).
Finding the Django source before this question, I also stumbled upon the following (which works):
SeparatedValuesField(models.TextField):
# ...
def value_from_object(self, obj):
return self.get_db_prep_value(super(SeparatedValuesField, self).value_from_object(obj))
I've been working through an ordered ManyToManyField widget, and have the front-end aspect of it working nicely:
Unfortunately, I'm having a great deal of trouble getting the backend working. The obvious way to hook up the backend is to use a through table keyed off a model with ForeignKeys to both sides of the relationship and overwrite the save method. This would work great, except that due to idiosyncrasies of the content, it is an absolute requirement that this widget be placed in a fieldset (using the ModelAdmin fieldsets property), which is apparently not possible.
I'm out of ideas. Any suggestions?
Thanks!
In regard to how to set up the models, you're right in that a through table with an "order" column is the ideal way to represent it. You're also right in that Django will not let you refer to that relationship in a fieldset. The trick to cracking this problem is to remember that the field names you specify in the "fieldsets" or "fields" of a ModelAdmin do not actually refer to the fields of the Model, but to the fields of the ModelForm, which we are free to override to our heart's delight. With many2many fields, this gets tricky, but bear with me:
Let's say you're trying to represent contests and competitors that compete in them, with an ordered many2many between contests and competitors where the order represents the competitors' ranking in that contest. Your models.py would then look like this:
from django.db import models
class Contest(models.Model):
name = models.CharField(max_length=50)
# More fields here, if you like.
contestants = models.ManyToManyField('Contestant', through='ContestResults')
class Contestant(models.Model):
name = models.CharField(max_length=50)
class ContestResults(models.Model):
contest = models.ForeignKey(Contest)
contestant = models.ForeignKey(Contestant)
rank = models.IntegerField()
Hopefully, this is similar to what you're dealing with. Now, for the admin. I've written an example admin.py with plenty of comments to explain what's happening, but here's a summary to help you along:
Since I don't have the code to the ordered m2m widget you've written, I've used a placeholder dummy widget that simply inherits from TextInput. The input holds a comma-separated list (without spaces) of contestant IDs, and the order of their appearance in the string determines the value of their "rank" column in the ContestResults model.
What happens is that we override the default ModelForm for Contest with our own, and then define a "results" field inside it (we can't call the field "contestants", since there would be a name conflict with the m2m field in the model). We then override __init__(), which is called when the form is displayed in the admin, so we can fetch any ContestResults that may have already been defined for the Contest, and use them to populate the widget. We also override save(), so that we can in turn get the data from the widget and create the needed ContestResults.
Note that for the sake of simplicity this example omits things like validation of the data from the widget, so things will break if you try to type in anything unexpected in the text input. Also, the code for creating the ContestResults is quite simplistic, and could be greatly improved upon.
I should also add that I've actually ran this code and verified that it works.
from django import forms
from django.contrib import admin
from models import Contest, Contestant, ContestResults
# Generates a function that sequentially calls the two functions that were
# passed to it
def func_concat(old_func, new_func):
def function():
old_func()
new_func()
return function
# A dummy widget to be replaced with your own.
class OrderedManyToManyWidget(forms.widgets.TextInput):
pass
# A simple CharField that shows a comma-separated list of contestant IDs.
class ResultsField(forms.CharField):
widget = OrderedManyToManyWidget()
class ContestAdminForm(forms.models.ModelForm):
# Any fields declared here can be referred to in the "fieldsets" or
# "fields" of the ModelAdmin. It is crucial that our custom field does not
# use the same name as the m2m field field in the model ("contestants" in
# our example).
results = ResultsField()
# Be sure to specify your model here.
class Meta:
model = Contest
# Override init so we can populate the form field with the existing data.
def __init__(self, *args, **kwargs):
instance = kwargs.get('instance', None)
# See if we are editing an existing Contest. If not, there is nothing
# to be done.
if instance and instance.pk:
# Get a list of all the IDs of the contestants already specified
# for this contest.
contestants = ContestResults.objects.filter(contest=instance).order_by('rank').values_list('contestant_id', flat=True)
# Make them into a comma-separated string, and put them in our
# custom field.
self.base_fields['results'].initial = ','.join(map(str, contestants))
# Depending on how you've written your widget, you can pass things
# like a list of available contestants to it here, if necessary.
super(ContestAdminForm, self).__init__(*args, **kwargs)
def save(self, *args, **kwargs):
# This "commit" business complicates things somewhat. When true, it
# means that the model instance will actually be saved and all is
# good. When false, save() returns an unsaved instance of the model.
# When save() calls are made by the Django admin, commit is pretty
# much invariably false, though I'm not sure why. This is a problem
# because when creating a new Contest instance, it needs to have been
# saved in the DB and have a PK, before we can create ContestResults.
# Fortunately, all models have a built-in method called save_m2m()
# which will always be executed after save(), and we can append our
# ContestResults-creating code to the existing same_m2m() method.
commit = kwargs.get('commit', True)
# Save the Contest and get an instance of the saved model
instance = super(ContestAdminForm, self).save(*args, **kwargs)
# This is known as a lexical closure, which means that if we store
# this function and execute it later on, it will execute in the same
# context (i.e. it will have access to the current instance and self).
def save_m2m():
# This is really naive code and should be improved upon,
# especially in terms of validation, but the basic gist is to make
# the needed ContestResults. For now, we'll just delete any
# existing ContestResults for this Contest and create them anew.
ContestResults.objects.filter(contest=instance).delete()
# Make a list of (rank, contestant ID) tuples from the comma-
# -separated list of contestant IDs we get from the results field.
formdata = enumerate(map(int, self.cleaned_data['results'].split(',')), 1)
for rank, contestant in formdata:
ContestResults.objects.create(contest=instance, contestant_id=contestant, rank=rank)
if commit:
# If we're committing (fat chance), simply run the closure.
save_m2m()
else:
# Using a function concatenator, ensure our save_m2m closure is
# called after the existing save_m2m function (which will be
# called later on if commit is False).
self.save_m2m = func_concat(self.save_m2m, save_m2m)
# Return the instance like a good save() method.
return instance
class ContestAdmin(admin.ModelAdmin):
# The precious fieldsets.
fieldsets = (
('Basic Info', {
'fields': ('name', 'results',)
}),)
# Here's where we override our form
form = ContestAdminForm
admin.site.register(Contest, ContestAdmin)
In case you're wondering, I had ran into this problem myself on a project I've been working on, so most of this code comes from that project. I hope you find it useful.
Ok, I am working on a Django application with several different models, namely Accounts, Contacts, etc, each with a different set of fields. I need to be able to allow each of my users to define their own fields in addition to the existing fields. I have seen several different ways to implement this, from having a large number of CustomFields and just mapping a custom name to each field used by each user. I have also seem recommendations for implementing complex mapping or XML/JSON style storage/retrieval of user defined fields.
So my question is this, has anyone implemented user defined fields in a Django application? If so, how did you do it and what was your experience with the overall implementation (stability, performance, etc)?
Update: My goal is to allow each of my users to create n number of each record type (accounts, contacts, etc) and associate user defined data with each record. So for example, one of my users might want to associate an SSN with each of his contacts, so I would need to store that additional field for each Contact record he creates.
Thanks!
Mark
What if you were to use a ForeignKey?
This code (untested and for demo) is assuming there is a system-wide set of custom fields. To make it user-specific, you'd add a "user = models.ForiegnKey(User)" onto the class CustomField.
class Account(models.Model):
name = models.CharField(max_length=75)
# ...
def get_custom_fields(self):
return CustomField.objects.filter(content_type=ContentType.objects.get_for_model(Account))
custom_fields = property(get_fields)
class CustomField(models.Model):
"""
A field abstract -- it describe what the field is. There are one of these
for each custom field the user configures.
"""
name = models.CharField(max_length=75)
content_type = models.ForeignKey(ContentType)
class CustomFieldValueManager(models.Manager):
get_value_for_model_instance(self, model):
content_type = ContentType.objects.get_for_model(model)
return self.filter(model__content_type=content_type, model__object_id=model.pk)
class CustomFieldValue(models.Model):
"""
A field instance -- contains the actual data. There are many of these, for
each value that corresponds to a CustomField for a given model.
"""
field = models.ForeignKey(CustomField, related_name='instance')
value = models.CharField(max_length=255)
model = models.GenericForeignKey()
objects = CustomFieldValueManager()
# If you wanted to enumerate the custom fields and their values, it would look
# look like so:
account = Account.objects.get(pk=1)
for field in account.custom_fields:
print field.name, field.instance.objects.get_value_for_model_instance(account)