How create model in Django with Field that contains items or object that created from another model - django-models

Hei, still low Django,
But l would like to create model field that can contain a drop-down of items/objects created from another model of another app.
Items found, l would like to select one to appear in the rendered template as it appeared in the other template, having all it's fields eg. game_name,game_link,game_picture
Please l request on your support. Any one to solve for such issue

Related

Search/Filter Dropdown in Django Admin Panel for standard CharField

I am using Django to build data models including a model Company. Some of the fields belonging to this model are limited to set choices using the choices='' argument. Some of these fields have a large number of choices, for example a country CharField which lists all countries. Finding the right value among the long list can be tedious so I want to be able to search across the given choice values. This is easy to do for ForeignKey/ManytoMany fields using autocomplete_fields = [] as seen in the attached screenshot (from a different model) but can't seem to find a method for implementing this with a normal CharField with lots of choices. This seems like something that should be built into the Django for the admin panel but I can't find anything within the docs. Please how can I implement a search/filter dropdown for any given (non FK/m2m) fields? Thanks in advance. If there's anymore information I can provide let me know and I will.
country = models.CharField(max_length=128, choices=COUNTRY_CHOICES, null=True)
Model code added. This COUNTRY_CHOICES array is what I would like to be able to select from in a searchable dropdown list.

ManyToManyField is not getting saved in Wagtail Page

I have a subclass of Wagtail Page class that has field of django ManyToManyField type. When I try to create a new instance of my page object, I get a list of objects that the the ManyToManyField points to and I am able select multiple items. However, after creating that page when I try to edit the same page, it seems no data got saved for the ManyToMany field. I know in Django ModelAdmin one have to override the save_related() to save ManyToMany field data. Is there a similar method for the Wagtail Page model?
You should define the field as a ParentalManyToManyField relation, as per the example here: http://docs.wagtail.io/en/v1.13.1/getting_started/tutorial.html#categories
This is a variant of ManyToManyField which is able to keep track of the relation in memory, allowing it to work in situations such as previewing and saving as draft (where it doesn't get saved to the normal database records).
I was able to use the 'after_edit_page' and 'after_create_page' hooks to save the data for the page's ManyToMany fields.

Angularjs How to manage Code

I am new to Angularjs. I have a page like this.
I have a single controller which does all the work which is currently working fine. I want to manage my code to use angular-given-functionalities by using filters, services, controllers etc.
In this page i have many sections i am mentioning here.
Search filters
Category
Attributes
Tags
Pagination Section plus Grid or List links
Sort plus Compare section
Product data to display
How can i break my code in separate chunks to manage it properly.
I have created a service which does all ajax related work.
Here is the dependency.
1. Search does nothing except page redirect
2. Category can have a view more which will update itself will more then 10 records
3. Attributes can have viewmore button to display more then 10 attributes. Attribute can also impect on the following
When we select single on multiple attribute it will change
1. Category
2. Attributes
3. Tags
4. Pagination
5. Listing (Product data to display)
4. Tags can be removed and can have excatly same behavior as Attributes
5. Pagination will change itself and product data display
6. Sort will have same as pagination
7. Nothing
I dont need code just some suggestions on how can i choose things from Angular to make managed code.

Custom Button to copy data from Opportunity into a related custom object

I have a custom object that is used for product setup that is mapped to an opportunity. It's a one to many relationship - one opportunity maps to many setup objects, but one setup object is only mapped to one opportunity.
Opportunity has some setup fields that need to act as defaults for the related custom object. Unfortunately, I cannot just specify them in a formula - getting an error.
What I would like to do is have a custom button that would allow user to click and copy all of the related setup fields from the opportunity into the custom setup object and then edit them as needed.
Any pointers or sample code are greatly appreciated!
You can achieve this with a custom button on the related list for your custom object on the opportunity detail page.
All of the fields on a standard Salesforce new/edit screen have id's associated with them. You can specify values for fields by using these ids to set GET parameters on your URL. For example if the id on the name field on your opportunity is 'opp3', the following URL will populate the name field on your new opportunity page:
https://na2.salesforce.com/006/e?opp3=Hello+World
You would have to change na2 to the correct server for your org.
The new record page URL contains the 3 character id prefix for your particular object and then '/e'. 006 is the prefix for opportunities. You will have to attempt to create a new record to see what the 3 characters are for your custom object.
You will have to capture the id's of the fields you want to populate on your custom object. You can do this by viewing the source of the new record page. For custom fields these id's will take the form of a Salesforce Id (eg. 00N40000002QhEV).
Create a new list button on your custom object and set the behavior to without header and sidebar and set the source to URL. Build up your URL with id=value pairs separated by '&' using the id you got from the page source and the insert field functionality to select the opportunity fields your wish to add in. You should end up with something like this:
/a0U/e?00N40000002QhEV={!Opportunity.Name}&00N40000002QhEW={!Opportunity.StageName}
a0U should be replaced by the correct prefix for your custom object. Then add your button to the related list for your custom object under opportunity.

Django: form that updates X amount of models

I have a page where it displays a filtered model instance list and allows users to update some fields of it or add new fields as a form.
I am curious what wpuld be a clever way of doing this, to delete and resave all the input data or make comparison for each data and save edited / new fields& entities.
I would like to mind you that I use postgres for saving these values and I display around 20 entries for this form.
The QuerySet object has the update() method - it's used in ie. Admin Panel for bulk updating multiple selected objects from change lists. Here is the method reference at django's official documentation.
How to use it:
Just create queryset with the models you want to update (assumng that MyModel has field named 'my_field'):
qs = MyModel.objects.all()
qs.update(my_field=value)
That's it - remember that update() method will not send any signals like the save() method - it will just run query directly to database.
As for 'adding fields via form' - I don't know if I got it right? You want to add additional related models or dynamically add fields to the model table on database?
If you want to add related models then use InlineFormset (http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-form) - it's quite easy to handle.
Otherwise you have to add fields to models' _meta as described here: How dynamic add custom field to model.

Resources