i'm currently writing tests for my django project and i need to make sure
that a specific field of a model is of a certain type.
for example in model Pictures i want to make sure that there is a field with the
name "image" and that he of type ImageField.
i also want to be able to check if an attribute is of type ForeignKey of
model Pictures.
i tried to use the assertIsInstance, but i need to assign the attribute or else
he is None.
does someone know how to do it?
To answer your question, a check like this should work:
from django.db.models import ImageField
from wherever.mymodels import Pictures
...
field = Pictures._meta.get_field("image")
assertTrue(isinstance(field, ImageField))
Though, for my 2¢, it may make more sense for your unit test to check that setting the "image" of some Picture instance works as-expected, rather than just checking for an ImageField named "image". After all, what you should really be checking with a test case is: "will the code I'm using to upload/validate/save images reliably work"?
Related
I created a project say foo. And created an app named admin. But it causes an error
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: admin”
So I read this and made changes as mentioned there. I marked the label as foo.admin.
I don't exactly know what the label is, but maybe this is the unique name given to an app to distinguish apps with the same name in case.
So is that mean, everywhere I have to use label instead of name?
But it causes another error.
String model references must be of the form 'app_label.ModelName'.
So I used the name in models as foo.admin in ForeignKey parameter. But the same error comes up. But the error persists.
I googled the error and found this. So I changed the ForeignKey parameter from foo.admin to admin. But in either case, I'm having this error.
So in short I want to ask
How to use apps with the same name in the same django project, like Which files are need to be modified and what to write ForeignKey parameters etc.?
(I'm using django 2.0)
I installed django haystack using whoosh. Everything works great, but I want to alter the names displayed next to the check boxes. I know they are generated using the verbose name set up on the models but I still have an issue with the 's' being added at the end of the names. I know there are custom forms and custom views but I am new to programming and some of the concepts do not make sense. I have also tried to search for any ideas but have had no luck. Any suggestions/advice?
Thanks in advance!
:)
is it not the label=_("Field_name") parameter in the checkbox field?
if its about the verbose name there is also verbose_name_plural which can be set up in models
I'm new to Django, and I haven't found the answer yet in the extensive documentation. I'm asking for pointers to research, not for working code. That being said, here's my problem:
In one of my models theres a BooleanField (it gets rendered in the admin form as a checkBox). Let's call it 'A'. It only makes sense to edit other field (say, CharField 'B') if A is checked.
So, is there a way to make B read only, or even changing its content to an empty string, dinamically, if A is checked? Thank you.
(Django 1.5.2, Python 2.7.5)
You're going to need several things to make this work. You may be able to skip some of them depending if you mainly care abut the UI, or the data integrity in the db.
Since the user can (presumably) check/uncheck Field A on the client-side you need some Javascript to enable/disable the appearance of Field B. These docs show how to load custom JS in your ModelAdmin class:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-asset-definitions
In your ModelForm you may want to do some check in the __init__ method against the value of self.instance.field_a and substitute some kind of ReadOnlyWidget for Field B for the initial display of the form. These docs show how to give your ModelAdmin a custom form class:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form
If you are writing some Javascript to do that dynamically it make be easier to skip this step and just do it client-side.
Finally you can use Django model validation to ensure that Field B is saved with a null value if Field A is checked:
https://docs.djangoproject.com/en/dev/ref/models/instances/#validating-objects
I have a app called C3po. I'm working on the model "Article". In this model I want to create a ForeignKey field with a custom related_name to a model in another app called Lea. To make sure the related_name is unique, I want to name it "c3po_articles". But I don't want to hard code the app name c3po. How can I get the folder / app name in a dynamic way ? Do I use __file__ and split it or is there a more elegant method ?
Thank you for your help :)
The related_name attribute supports automatic string interpolation with two variables: app_label and class. For example:
models.ForeignKey(FooModel, related_name='%(app_label)s_%(class)s_foo')
Now, I'm honestly not sure if Django will let you just include one or the other, i.e. just '%(app_label)s_foo', but you can try. (After a closer look at the docs, I highly doubt it. It seems like it's both or neither, but still, test it yourself and see.)
See: https://docs.djangoproject.com/en/dev/topics/db/models/#be-careful-with-related-name
EDIT
Actually, after thinking about it more, for your case, you could just use '%(app_label)s_%(class)ss', which should net you c3po_articles as the related_name.
This is really frustrating,
I can't and can't find how to create a form (I'm guessing a forms.Form form) to update just one field of a more complex model.
The model has 5 fields, and a form to create, update all of them.
But in a different case i need to let the user update only the title (a field in the model), so i need tried so many things until now (including creating an HTML form by hand and from the view to save it, creating a forms.Form and many more, nothing seem to work), There is no code here because i don't even know which one to put....
Maybe some one can help me with that, I'm sure it is a simple thing, But for some reason i am stuck on this for a long time...
Thank you,
Erez
If you're using ModelForms, you just have to define a fields attribute in Meta as a tuple containing just the names of the fields you want. See the documentation.