Django ordered ManyToManyField in admin interface - django

I have a legacy database with tables for documents and authors. A third table defines an ordered many to many relationship between the documents and authors, using foreign keys to the documents and the authors and an integer to specify the author order for a given document.
Using Django 1.1.1 (or SVN), is there a way to edit the document authors and their order in an admin page?

This is quick and a bit rough, but it should get you close(r).
from django.db import models
from django.contrib import admin
class Document(models.Model):
name = models.CharField(max_length = 128)
def __unicode__(self):
return self.name
class Author(models.Model):
name = models.CharField(max_length = 128)
document = models.ManyToManyField(Document, through = 'Foo')
def __unicode__(self):
return self.name
class Foo(models.Model):
document = models.ForeignKey(Document)
author = models.ForeignKey(Author)
author_order = models.IntegerField()
class FooInline(admin.TabularInline):
model = Foo
class DocumentAdmin(admin.ModelAdmin):
inlines = [ FooInline ]
admin.site.register(Author)
admin.site.register(Document, DocumentAdmin)
http://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects

this might help you too - there is some utility in Django to build models for the legacy databases.
I've built a model for mediawiki database using it. It gets most of the things right, but you'll need to tweak the models a little.

Related

djongo ArrayField not appearing in Django Admin

I am following the official docs of Djongo mapper => https://www.djongomapper.com/using-django-with-mongodb-array-field/ to add Array Fields to my Model, but unfortunately even after adding the Array Fields as stated in the docs I am unable to see them in the below view shown in the docs.
Here is the Model defined by me.
from djongo import models
from django import forms
# Create your models here.
class Author(models.Model):
name = models.CharField(max_length=200)
email = models.EmailField()
class Meta:
abstract = True
def __str__(self):
return self.name
class EventModel(models.Model):
_id = models.ObjectIdField()
authors = models.ArrayField(
model_container=Author,
)
def __str__(self):
return self._id
Here is the generated Django Admin view based on my Models, which is no way similar to the one shown in docs here.
https://www.djongomapper.com/using-django-with-mongodb-array-field/
Any help on this would be appreciated thanks :)

Django admin multiple querysets on one page

How to have multiple tables(queryset) on one page in django admin.
For example:
When i go to the company page, i can see the list of departments in the company, i can also see the list of employees in the company.
You can use InlineModelAdmin objects to implement this, although if you want to do nested inlines you should check out this post. Although as that post says:
...it would be a kind of convoluted design to implement.
You didn't provide any code here so the best I can do is guess your model relationships.
models.py
from django.db import models
class Department(models.Model):
name = models.CharField(max_length=250)
...
class Employee(models.Model):
name = models.CharField(max_length=250)
...
class Company(models.Model):
name = models.CharField(max_length=250)
departments = models.ForeignKey(Department)
employees = models.ForeignKey(Employee)
...
admin.py
from django.contrib import admin
class EmployeeInline(admin.StackedInline):
model = Employee
class DepartmentInline(admin.StackedInline):
model = Department
class CompanyAdmin(admin.ModelAdmin):
list_display = ('name')
inlines = [DepartmentInline, EmployeeInline]
admin.site.register(CompanyAdmin)

Add new rows to related model on creation of parent model in Admin

I have models for adding products. The name of the products are in several languages, so I made a on-to-many raltion with a 'Name'-model.
This is my models
class Product(models.Model):
active = models.BooleanField()
class ProductName(models.Model):
productName = models.CharField(max_length=250)
product = models.ForeignKey('Product', on_delete=models.CASCADE)
language = models.ForeignKey('Language', on_delete=models.CASCADE)
def __str__(self):
return self.productName
class Language(models.Model):
language = models.CharField(max_length=55)
languageAbbreviation = models.CharField(max_length=10)
def __str__(self):
return self.language
Now in the admin page of mysite, I want to add product names on creation of a product.
I tried some misarable attempt with some thing I found about 'admin.TabularInline'. But I think that is wrong because nothing is working with that.
Any suggestion about how to solve this is much appreciated!
A model admin like this:
class ProductAdmin(admin.ModelAdmin):
class ProductNameInline(admin.TabularInline):
model = ProductNameInline
fields = ['productName', 'language']
model = Product
inlines = [ProductNameInline]
should provide you with a page that allows you set the name(s) of a product.
Make sure all the necessary static files for the javascript are available.

Django admin GenericForeignKey widget

I'm creating a Django app where all the models can be related to each other in an order set by the user. I'm setting all this up using GenericForeignKeys. The kicker is that I need to be able to support multiple collections of these types of relationship/admin. So one object can have a more than one collection of related objects.
Does anyone know of a good GenericForeignKey widget for this situation? Preferably, it would be an autocomplete search that populates the admin form since I can end up having a large number of objects.
Here is the code for my app to get a better idea of what I mean.
from django.contrib import admin
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django import forms
# Models
class Base(models.Model):
title = models.CharField(max_length=255)
class Meta:
abstract = True
def __unicode__(self):
return self.title
class Related(Base):
""" A generic relationship model for relating resources.
"""
order = models.IntegerField(blank=True, null=True)
limit = models.Q(model = 'Apple') | models.Q(model = 'Orange') | models.Q(model = 'Pear')
content_type = models.ForeignKey(ContentType, related_name="related_%(class)s")
object_id = models.PositiveIntegerField(db_index=True)
object = generic.GenericForeignKey()
related_content_type = models.ForeignKey(ContentType, related_name="related_related_%(class)s", limit_choices_to = limit)
related_object_id = models.PositiveIntegerField(db_index=True)
related_object = generic.GenericForeignKey('related_content_type', 'related_object_id')
class Meta:
ordering = ('order',)
abstract = True
def __unicode__(self):
return self.object.title
class FreshFruit(Related):
pass
class OldFruit(Related):
pass
class Apple(Base):
pass
class Orange(Base):
pass
class Pear(Base):
pass
# Admin classes
class FreshFruitInline(generic.GenericStackedInline):
model = FreshFruit
extra = 1
# Admin classes
class OldFruitInline(generic.GenericStackedInline):
model = OldFruit
extra = 1
class AppleAdmin(admin.ModelAdmin):
inlines = [FreshFruitInline, OldFruitInline,]
admin.site.register(Apple, AppleAdmin)
class OrangeAdmin(admin.ModelAdmin):
inlines = [FreshFruitInline, OldFruitInline,]
admin.site.register(Orange, OrangeAdmin)
class PearAdmin(admin.ModelAdmin):
inlines = [FreshFruitInline, OldFruitInline,]
admin.site.register(Pear, PearAdmin)
I've searched and searched, and found widgets that do this for a ManyToMany relationship, but nothing for my situation.
Thanks for taking the time to look at this.
Have a look at Grappelli's generic foreign key widget, which works well:
django-grappelli/generic_2_2

django - many to many field as dropdown on form

I have a many to many relationship model which actually shows as a multi select list on forms. In one particular place I want to show it as dropdown single selection - any idea how to do this?
See the documentation on overriding default field types or widgets.
If you've got a Book model, with a ManyToMany relationship to Author, like this:
class Author(models.Model):
name = models.CharField(max_length=100)
title = models.CharField(max_length=3, choices=TITLE_CHOICES)
def __unicode__(self):
return self.name
class Book(models.Model):
name = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
then you can do something like this:
from django.forms import ModelForm, Select
class AuthorForm(ModelForm):
class Meta:
model = Author
widgets = {
'name': Select(),
}
NB. This code is not tested, but will hopefully be enough to get you on your way.