How do you model this in django? - django

Considering the class model as follows:
alt text http://www.forteresse.net/site/stackoverflow/classes.png/image
How do you do this in models.py?
class House(models.Model):
foo = models.CharField(max_length=123)
class Caravan(models.Model):
foo = models.CharField(max_length=123)
class Door(models.Model):
bar = models.CharField(max_length=123)
house = models.ForeignKey(House)
caravan = models.ForeignKey(Caravan)
But these foreign key definitions may not be what is intended. How do you code this in django? The intention is to reuse the same model "Door" for both "House" and "Caravan".
After digging deeper, I found this; is this the right way to model the problem?
class House(models.Model):
foo = models.CharField(max_length=123)
class Caravan(models.Model):
foo = models.CharField(max_length=123)
class Door(models.Model):
bar = models.CharField(max_length=123)
house = models.ForeignKey(House, null=True, blank=True)
caravan = models.ForeignKey(Caravan, null=True, blank=True)

class Door(models.Model):
bar = models.CharField(max_length=123)
class Caravan(models.Model):
foo = models.CharField(max_length=123)
doors = models.ManyToManyField(Door)
class House(models.Model):
foo = models.CharField(max_length=123)
doors = models.ManyToManyField(Door)

I think you should try:
class Door(models.Model):
bar = models.CharField(max_length=123)
class House(models.Model):
foo = models.CharField(max_length=123)
door = models.ForeignKey(Door)
class Caravan(models.Model):
foo = models.CharField(max_length=123)
door = models.ForeignKey(Door)

Can you clarify what you're really looking for with an example query? It's not clear to me. This is what I think you're looking for:
class Door(models.Model):
bar = models.CharField(max_length=123)
class House(Door):
foo = models.CharField(max_length=123)
class Caravan(Door):
foo = models.CharField(max_length=123)
Then you can do things like Caravan.objects.values('foo','bar')

Related

C++ - Can I pass child class as parameter instead of base class

I have this:
class DATABASE_API MySQLConnection
{
}
And then a child class:
class DATABASE_API WorldDatabaseConnection : public MySQLConnection
{
}
Then I have this:
class GameDatabase {
public:
GameDatabase(PreparedStatement<MySQLConnection>* preparedStatement, GameDatabaseFlags gdf)
{
_gameObjectDatabaseFlag = gdf;
_preparedStatement = preparedStatement;
};
GameDatabaseFlags _gameObjectDatabaseFlag;
protected:
uint32_t versionId;
virtual void MapGameDatabase(GameDatabaseContainer gameDatabaseContainer) = 0;
PreparedStatement<MySQLConnection>* _preparedStatement;
};
When I try to initialize GameDatabase like so:
PreparedStatement<WorldDatabaseConnection> * stmt = WorldDatabase.GetPreparedStatement(WORLD_SEL_GAMEOBJECTS_TEMPLATE);
auto gameDatabase = new GameDatabase(stmt,GAMEOBJECTS_DB);
I get the following error:
No matching constructor for initialization of 'GameDatabase'
Why is that? Can't I simple use the child class WorldDatabaseConnection in place of the base class MySQLConnection?
Even though WorldDatabaseConnection is a child class of MySQLConnection, the template class PreparedStatement<WorldDatabaseConnection> is not a child class of PreparedStatement<MySQLConnection>.

DRF: how to call a serializer (as a nested serializer field) before it is defined?

class ASerializer(serializers.ModelSerializer):
b = Bserializer()
...
class BSerializer(serializers.ModelSerializer):
...
I need to call BSerializer before it is defined. How do I do that?
class ASerializer(serializers.ModelSerializer):
def __init__(self, instance=None, **kwargs):
super().__init__(instance, **kwargs)
self.fields['b'] = BSerializer()
class BSerializer(serializers.ModelSerializer):
...

Play 2.1.1 Bind a List<Class> from form

I saw some similar questions, but I couldn't really understand any answers.
I have a problem with getting a list of from a form. I've been doing it like this:
#Entity
public class Foo extends Model {
#Id
public Long id;
#Required
#Valid
#ManyToMany
public static List<Bar> bars = new ArrayList<Bar>();
}
#Entity
public class Bar extends Model{
#Id
public int id;
#Required
public String name;
// #Required
#Valid
#ManyToMany
public static List<Foo> foo = new ArrayList<Foo>();
}
And i've tried to make the interface based on answers i've read
#form(routes.Application.newFoo()) {
<select multiple name = "bar[].name">
<option name = "bars[].name" value="option1">Option1</option>
<option name = "bars[].name" value="option2">Option2</option>
<option name = "bars[].name" value="option3">Option3</option>
</select>
...
}
But when i try to do
Form filledForm = fooForm.bindFromRequest();
if (filledForm.hasErrors()) {
return badRequest(views.html.index
.render(Problem.all(), filledForm));
} else {
Foo foo = filledForm.get();
foo gets all other atributes, but the list is always empty.
What should I do?

Django: define a name for reverse ForeignKey

I have two models:
class Foo(models.Model):
foo_field = ...
class Bar(models.Model):
foo = models.ForeignKey(Foo, on_delete=models.CASCADE)
bar_field = ...
and I can access all Bar instances related to a Foo with:
Foo.bar_set.all()
Is there a way to change the 'reverse name', like in ManyToManyField, so that I can write:
Foo.bars.all()
?
yes, using related_name
class Foo(models.Model):
foo_field = ...
class Bar(models.Model):
foo = models.ForeignKey(Foo, related_name="bars", on_delete=models.CASCADE)
bar_field = ...

Django - ManyToOne relation to a child class

Is there a way to declare this case so that it works? I hope the code is self-explanatory.
class A(Model):
many_to_one = models.ForeignKey(B)
(...)
class B(A):
(...)
class A(Model):
many_to_one = models.ForeignKey('B')
(...)
class B(A):
(...)