foreign-keys

Foreign keys in django admin list display

寵の児 提交于 2019-11-29 20:37:24
If a django model contains a foreign key field, and if that field is shown in list mode, then it shows up as text , instead of displaying a link to the foreign object. Is it possible to automatically display all foreign keys as links instead of flat text? (of course it is possible to do that on a field by field basis, but is there a general method?) Example : class Author(models.Model): ... class Post(models.Model): author = models.ForeignKey(Author) Now I choose a ModelAdmin such that the author shows up in list mode: class PostAdmin(admin.ModelAdmin): list_display = [..., 'author',...] Now

Manipulating Data in Django's Admin Panel on Save

北城余情 提交于 2019-11-29 20:36:47
Ok, so here's the skinny: # models.py class Article( models.Model ): title = models.CharField( max_length = 255 ) author = models.ForeignKey( User ) published_at = models.DateTimeField( auto_now_add = True ) body = models.TextField( ) def __unicode__( self ): return self.title # admin.py from hpccoe.news.models import Article from django.contrib import admin from django import forms from django.forms import widgets class ArticleAdminForm( forms.ModelForm ): title = forms.CharField( max_length = 255, required = True ) body = forms.CharField( required = True, widget = widgets.Textarea ) class

Django “Cannot add or update a child row: a foreign key constraint fails”

青春壹個敷衍的年華 提交于 2019-11-29 19:48:11
I have a model Coupon , and a model Photo with a ForeignKey to it: class Photo(models.Model): coupon = models.ForeignKey(Coupon, related_name='description_photos') title = models.CharField(max_length=100) image = models.ImageField(upload_to='images') I set up inlines in the admin so now I'm able to add photos to a coupon from the admin. I attempt to add one, and upload is successful, but then I get Django's debug page with this error: IntegrityError at /admin/coupon/coupon/321/ (1452, 'Cannot add or update a child row: a foreign key constraint fails (`my_project`.`coupon_photo`, CONSTRAINT

What is a proper naming convention for MySQL FKs?

隐身守侯 提交于 2019-11-29 18:56:06
Being that they must be unique, what should I name FK's in a MySQL DB? In MySQL, there is no need to give a symbolic name to foreign key constraints. If a name is not given, InnoDB creates a unique name automatically. In any case, this is the convention that I use: fk_[referencing table name]_[referenced table name]_[referencing field name] Example: CREATE TABLE users( user_id int, name varchar(100) ); CREATE TABLE messages( message_id int, user_id int ); ALTER TABLE messages ADD CONSTRAINT fk_messages_users_user_id FOREIGN KEY (user_id) REFERENCES users(user_id); I try to stick with the same

FK on a single column referencing a column from composite PK

一个人想着一个人 提交于 2019-11-29 18:00:47
Not able to create /find the logic to apply FK on a column in child table referencing a column from composite PK of parent table. create table product(prod_id number, prod_name varchar2(20), price number, constraint PK12 primary key(prod_id,prod_name)); Table created. create table purchase(prod_id number, purchase_price number, constraint FK12 foreign key(prod_id) references product(prod_id)); create table purchase(prod_id number, purchase_price number, constraint FK12 foreign key(prod_id) references product(prod_id)) ERROR at line 1: ORA-02270: no matching unique or primary key for this

MongoDB DBRef ON DELETE CASCADE

随声附和 提交于 2019-11-29 17:43:41
问题 Is there a way in MongoDB to have a foreign key with a 'ON DELETE CASCADE' functionality? I know you can use DBRef as a sort of foreign key but when the item in a collection where the reference points to is removed, the reference returns null. But i want that the item where the reference belongs to gets removed. How do i do this? Or do i need every time i remove things check references to it? 回答1: This feature doesn't exist now. If you want it. Add it on MongoDB Bugtracker http://jira.mongodb

Django models filter by foreignkey

≡放荡痞女 提交于 2019-11-29 17:26:46
问题 I'm having some trouble in filtering objects from a set of models. Here is the problem: I have 3 classes: class Autor(models.Model): nome = models.CharField(max_length=50) slug = models.SlugField(max_length=50, blank=True, unique=True) foto = models.ImageField(upload_to='autores/', null=True, blank=True) ... class CategoriaRecolha(models.Model): categoria = models.CharField(max_length=30) descricao = models.TextField() slug = models.SlugField(max_length=30, blank=True, unique=True) ... class

Laravel 5.1: Enable SQLite foreign key constraints

◇◆丶佛笑我妖孽 提交于 2019-11-29 17:22:57
问题 In SQLite, foreign key constraints are disabled by default. What's the best way to configure Laravel 5.1's SQLite database connection to enable foreign key constraints? I don't see a way of doing this in ['connections']['sqlite'] in /config/database.php. 回答1: Here's one solution. In the boot() method of App\Providers\AppServiceProvider, add: if (DB::connection() instanceof \Illuminate\Database\SQLiteConnection) { DB::statement(DB::raw('PRAGMA foreign_keys=1')); } Thanks to @RobertTrzebinski

How to update MongoDB documents with arrays of sub-documents

旧时模样 提交于 2019-11-29 17:15:53
I am working with a MongoDB database whose collections model classes , students , subjects , and [academic] performance . Below are the Mongoose based schema and models: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var ObjectId = Schema.Types.ObjectId; // SUBJECT collection var subjectSchema = new Schema({ name : { type: String, required: true }, category : { type: String, required: true } }); var Subject = mongoose.model('Subject', subjectSchema); // STUDENT collection var studentSchema = new Schema({ name : { type: String, required: true }, grade : { type: Number,

FOREIGN KEY ON DELETE RESTRICT Error - Oracle

微笑、不失礼 提交于 2019-11-29 16:55:46
Lately I have been trying to add the following foreign key in the table, with the RESTRICT Clause in Oracle with the following command.: ALTER TABLE Employee_SalHead ADD CONSTRAINT PAYROLL_SHEAD_FKEY FOREIGN KEY ( SalHead_ID ) REFERENCES SalHead ( SalHead_ID ) ON DELETE RESTRICT ENABLE; This gave me the following error: Error starting at line : 11 in command - ALTER TABLE Employee_SalHead ADD CONSTRAINT PAYROLL_SHEAD_FKEY FOREIGN KEY ( SalHead_ID ) REFERENCES SalHead ( SalHead_ID ) ON DELETE RESTRICT ENABLE Error report - SQL Error: ORA-00905: missing keyword 00905. 00000 - "missing keyword"