-1

Trying to get some Django skills. I would like to have a class with one field being multiplied. So I can have more then one connected to my main class with an option of being active or not (for future needs).
So my subclass would look something like this:

class Subclass(models.Model):

  STATUS=(
  ('A', Active),
  ('U', Unactive)
  )
  status = modelsCharField(max_length=1, choices=STATUS)
  name = models.CharField(some options)

On main class I would like to call it as a reference but if I go for :

field=models.ManyToManyField(Subclass)

It is represented as a second table and for each entity I have to chose from all the entities of it. So if I have 2k entities in subclass I have to scroll true all of them to find my connection (in default admin page for instance)

I do not want that. All I need is 2 out of 2k entities that are connected and displayed in admin. And later on anly those with status Active would be displayed on the page itself.

So I figured I shall try a ForeignKey relation:

field=models.ForeignKey(Subclass, on_delegate=models.CASCADE)

This gives me errors about missing default values during migration though. Any help would be welcome, since I am a bit stuck right now.

Grumdrig
  • 16,588
  • 14
  • 58
  • 69
Jan Mejor
  • 141
  • 3
  • 15
  • What are you actually trying to accomplish? Your code has a lot of typos and formatting issues as well. Does your main class (main model), need to point to multiple records of your subclass (sub model)? – nael Dec 21 '16 at 22:29
  • This are just examples, typos are my thing. Have to fight with them all the time :( My task is to code something like ... adress book? So you have a person and his adress and also his old adress. So main class i a person, subclass is a list of adresses. But I need only those related. – Jan Mejor Dec 21 '16 at 23:42
  • 1
    if something like person and addresses, Address would have a ForeignKey(Person), and that will set you up for what you are after. Use the InlineModalAdmin. You likely don't want to be using many to many in this case as each address belongs to one person (likely). Pizza and toppings is an example of many to many (toppings on many different pizza types) https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#inlinemodeladmin-objects documentation for a way to handle in the admin. – AMG Dec 22 '16 at 01:14
  • @AMG oohhhh.... alright. – Jan Mejor Dec 22 '16 at 21:20

1 Answers1

0

Big thanks to #AMG. I did as you sugested and now it works. In my models.py

class Person(models.Model):
name = models.CharField(max_length=200, default="Name")
phone = models.CharField(max_length=12, default="+22123456789")
...
class Adress(models.Model):
...
person = models.ForeignKey(Person, on_delete=models.CASCADE)
...

And my admin.py

from django.contrib import admin
from .models import Adress
from .models import Person

class AdressInline(admin.TabularInline):
model = Adress

class PersonAdmin(admin.ModelAdmin):
inlines = [
AdressInline    
]

admin.site.register(Person, PersonAdmin)

This lets me to add a Person object and add an adress for it on a same place. All I have to do now i formating the lists.

Jan Mejor
  • 141
  • 3
  • 15